Benchmark topic

forEach vs for Loop JavaScript Performance Benchmark

Benchmark JavaScript forEach versus for loops online with realistic data, shareable jsPerf results, and browser plus runtime performance analysis.

Browser
runs
Runtime
signals
Share
URL
jsperf.net/run
Benchmark suite

forEach vs for Loop JavaScript Performance Benchmark

test A8.42M ops/sec
test B6.18M ops/sec
test C4.91M ops/sec
Browser
ops/sec
V8
JIT
QuickJS
baseline
Common performance question
Array iteration is one of the most searched JavaScript benchmark topics.
Workload-sensitive answer
The fastest loop can change with input size, callback work, runtime, and optimization state.
Best answered by rerunning
A saved jsPerf page lets you rerun the comparison as browsers and runtimes change.

What to include in the test

A fair iteration benchmark compares equivalent work.

Generate the array in setup so allocation does not dominate every test case.

Have both implementations produce the same result so dead-code elimination is less likely to distort the result.

Try small, medium, and large input sizes if your production workload varies.

What to avoid

Iteration benchmarks are easy to make misleading.

Do not compare a loop that does less work with a callback that does more work.

Do not generalize one browser result to every runtime and hardware profile.

Do not trade away readability unless the benchmark maps to a real hot path.

Benchmark clarity

Frequently asked questions

Short answers for searchers, with enough context to help them create a better benchmark.

Is forEach slower than a for loop in JavaScript?

It often can be slower for hot array loops because each iteration goes through a callback, but the real answer depends on the workload, runtime, input size, and surrounding code.

Should I always replace forEach with for loops?

No. Use readable code by default. Consider a for loop when the measured difference matters for a real performance-sensitive path.

How should I benchmark forEach vs for?

Use the same input array, put data generation in setup, make both test cases perform equivalent work, and rerun the benchmark in the environments you care about.

Can jsPerf compare array iteration across runtimes?

Yes. Browser results are available directly, and Deep Analysis plus the optional worker can add QuickJS, V8, Node, Deno, and Bun signals.