for vs forEach
JavaScript performance comparison
Info
Is it faster to use the native forEach or just loop with for?
Preparation code
<script>
var i, values = [],
sum = 0;
for (i = 0; i < 10000; i++) {
values[i] = i;
}
function add(val) {
sum += val;
}
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
forEach |
|
pending… |
for loop, simple |
|
pending… |
for loop, cached length |
|
pending… |
for loop, reverse |
|
pending… |
for loop, cached length, no callback |
|
pending… |
for loop, cached length, using function.call and supplying index and original array |
|
pending… |
Compare results of other browsers
Revisions
You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:
- Revision 2: published by Adrian Sutton
- Revision 3: published by Adrian Sutton
- Revision 5: published by Adrian Sutton
- Revision 9: published by alk
- Revision 10: published
- Revision 12: published
- Revision 15: published
- Revision 16: published by RubaXa
- Revision 17: published
- Revision 20: published by Brandon Satrom
- Revision 24: published
- Revision 26: published by cfddream
- Revision 27: published by cfddream
- Revision 31: published by spongman
- Revision 32: published by James Pamplin
- Revision 36: published
- Revision 37: published
- Revision 38: published by Sujay
- Revision 46: published by wicked
- Revision 47: published
- Revision 48: published by smith
- Revision 49: published by smith
- Revision 52: published
- Revision 53: published by Jean Vincent
- Revision 54: published
- Revision 56: published
- Revision 57: published by Gajus
- Revision 58: published
- Revision 59: published
- Revision 60: published
- Revision 61: published and last updated
- Revision 62: published
- Revision 63: published
- Revision 64: published
- Revision 65: published
- Revision 66: published by gerhard
- Revision 67: published
- Revision 68: published
- Revision 69: published
- Revision 70: published
- Revision 71: published
- Revision 75: published
5 comments
Using "++i" and "--i" should be more optimized (than "i++" and "i--") in terms of an interpreted language. Currently I'm not being able to find a pointer to further support this, but I'll be glad to craw for it again if it sounds interesting.
I got an exception while running in IE8, after the "Run tests again" finishes: Line: 81 Error: 'document' is null or not an object
I also got a similar exception when using IE9 platform preview 4: Line: 81 Error: 'undefined' is null or not an object
These are probably browser-specific issues which might not interfere with the test results but deserve some sort of investigation! :-)
I just tested and i++/i-- is faster than ++i/--i.
i++ should always be slower in programming; but compiler optimization can allow it to be as fast (but never faster) as ++i... only poorly created javascript engines does that.
i++ says to do either one of two things: A) temp = i; ++i; return temp; B) i++ {code1} {code2} --> {code1} ++i {code2}
So in the case of B, somevar[i++] = i; gets optimized to somevar[i] = ++i;
Now why is there a speed difference sometimes? The same reason that i+=1 does not always yield a slower result than ++i or i++. CHEATING.
Effectively, while i++ can be optimized to ++i; you have compiler optimizations that target only specific code segments (target the words, not the function).
Now these optimizations are often added in to target "javascript test suits." Rather than actually improve performance, you improve your score. Major differences as there are a plethora of cheats you can do to optimize iterations over loops; but actually optimizing "Real World Code" is something else.
Still... forEach should be much faster. After all, you are removing all the property calls and can directly increment along the memory allocated for the array!
I just can't work out why creating a custom forEach can be SO much faster (in chrome at least) -> http://jsperf.com/foreachvscustomforeach I would have thought the browser would have optimized most forEach loops to basically be for() loops anyway?