3 ms·
jsperf benchmarks of small highly optimizable loops are no better than random numbers. they aren't accurate. It is true, however, that array.forEach can theore
by kevingadd 13y ago
jsperf benchmarks of small highly optimizable loops are no better than random numbers. they aren't accurate.
It is true, however, that array.forEach can theoretically be worse than a hand-written loop - and I think in some cases it currently is. This is due to the fact that a call through Array.forEach might have to enter and exit C++ code, instead of remaining entirely in JS, which prevents optimizations like inlining and invariant code motion.
However, these problems will go away over time as most JS builtins are moved to pure JS implementations (which enables full optimizations).
If you are trying to determine things like 'what's the fastest way to iterate a sequence', the ONLY REALISTIC WAY to do this is to benchmark your actual use case, in context and see which approach is faster. Microbenchmarks applied to a language like JavaScript are, 99% of the time, complete horseshit.