Javascript for-loop microbenchmark

2024-07-20

Trying out a little for-loop microbenchmark

Written by: John Pitney

A colleague and I got discussing the various ways to iterate over elements of a Javascript Array. He’d heard that a C-style for loop was the fastest.

I ran some simple benchmarks; my script and results are in the gist. He had heard right! This benchmark has just about the shortest loop body possible. Any substantial loop body would likely make these differences insignificant. The usual advice about readability, doing your own benchmarks, and optimization apply. Bun had an edge over Node.js except when doing for..of.

Check out the benchmark script and results.