Loops
JavaScript performance comparison
Info
Compares speed of variations of the for, for-in, while, do-while, and Array.forEach looping constructs.
Changes since revision 22
: Fixed the "Reverse do … while loop" test so it doesn't make the arr array larger for later tests.
: Normalized operation complexity to arr[i] *= 1 for all tests so later iteration of tests aren't doing calculations with larger values (i.e. 1998 * 5 => 9,990 * 5 => 49,950).
: Fixed the "for .. in (will skip empty values)" test so it only uses arr's elements and excludes standard and injected Array members.
: Fixed the Preparation code so it doesn't leak its local "k" loop counter variable into the global scope.
Preparation code
<script>
var arr = [];
(function populateWithoutPollutingGlobalScope () {
var k = 2000;
while (k--) { arr[k]=k; }
})();
// Intentionally pollute all objects for the for-in test(s):
Object.prototype[Number.MIN_VALUE] = "Externally polluted.";
</script>
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
while loop that imitates a for loop |
|
pending… |
while loop that imitates a for loop, caching the length |
|
pending… |
Reverse while loop |
|
pending… |
Reverse while loop without implicit ToBoolean |
|
pending… |
Reverse do … while loop |
|
pending… |
Reverse for loop |
|
pending… |
Old ’n’ busted for loop |
|
pending… |
Old ’n’ busted for loop, caching the length |
|
pending… |
Cool guy loop |
|
pending… |
Cool guy loop, caching the length |
|
pending… |
Native Array#forEach implementation |
|
pending… |
Native Array#forEach implementation with named function |
|
pending… |
for .. in (will skip empty values) |
|
pending… |
i++ |
|
pending… |
Duff would be proud |
|
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 1: published by Mathias Bynens and last updated
- Revision 2: published
- Revision 4: published by Thomas Aylott and last updated
- Revision 6: published by WebReflection and last updated
- Revision 9: published by JFSIII and last updated
- Revision 10: published by Thomas Aylott
- Revision 17: published
- Revision 18: published by Lockal
- Revision 19: published by Lockal
- Revision 20: published
- Revision 21: published
- Revision 22: published
- Revision 23: published by Mike Lee
- Revision 24: published
- Revision 26: published by match vs split
- Revision 27: published by John-David Dalton and last updated
- Revision 29: published by Lucas Cepeda
- Revision 30: published
- Revision 31: published
- Revision 32: published
- Revision 33: published
- Revision 34: published by FrancescoMM
- Revision 35: published by Elijah Manor
- Revision 38: published by D. S. Schneider
- Revision 39: published
- Revision 40: published
- Revision 41: published by D. S. Schneider
- Revision 42: published
- Revision 43: published
- Revision 44: published
- Revision 46: published
- Revision 47: published
- Revision 48: published by Justin Bangerter
- Revision 49: published by Conor Malone
- Revision 50: published by Conor Malone
- Revision 51: published by Tri
- Revision 52: published by Raul
- Revision 53: published
- Revision 54: published
- Revision 55: published
- Revision 56: published
- Revision 58: published
- Revision 59: published
- Revision 60: published
- Revision 61: published
- Revision 62: published
- Revision 63: published
- Revision 64: published
- Revision 66: published
- Revision 67: published
- Revision 68: published
- Revision 69: published
- Revision 70: published
- Revision 71: published
- Revision 72: published
- Revision 73: published by wfreytag
- Revision 75: published
- Revision 76: published
- Revision 77: published
- Revision 78: published
- Revision 79: published
- Revision 80: published
- Revision 81: published by eTagTechnologies.Inc
- Revision 82: published by JeanHuguesRobert
- Revision 83: published
- Revision 84: published by Simo
- Revision 85: published
- Revision 86: published by Matt
- Revision 87: published
0 comments