Loops

JavaScript performance comparison

Revision 240 of this test case created by

Preparation code

<script>
  
</script>
      
<script>
Benchmark.prototype.setup = function() {
  var arr = [], r = 0
    for (var x = 0; x < 10000; x++)
      arr[x] = "a" + x

};
</script>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
while loop that imitates a for loop
var i = 0;
while (i < arr.length) {
 r = arr[i]++;
 i++;
};
pending…
while loop that imitates a for loop, caching the length
var i = 0,
    len = arr.length;
while (i < len) {
 r = arr[i]++;
 i++;
};
pending…
Reverse while loop
var i = arr.length;
while (i--) {
 r = arr[i]++
};
pending…
Reverse while loop without implicit ToBoolean
var i = arr.length;
while (i-- > 0) {
 r = arr[i]++
};
pending…
Reverse do … while loop
var i = arr.length;
do {
 r = arr[i]++
} while (i--);
pending…
Reverse for loop
for (var i = arr.length; i--;) {
 r = arr[i]++
};
pending…
Old ’n’ busted for loop
for (var i = 0; i < arr.length; ++i) {
 r = arr[i]++
};
pending…
Old ’n’ busted for loop, caching the length
for (var i = 0, len = arr.length; i < len; ++i) {
 r = arr[i]++
};
pending…
Cool guy loop
for (var i = -1; ++i < arr.length;) {
 r = arr[i]++
};
pending…
Cool guy loop, caching the length
for (var i = -1, len = arr.length; ++i < len;) {
 r = arr[i]++
};
pending…
Native Array#forEach implementation
arr.forEach(function(x) {
 r = x++
});
pending…
Native Array#forEach implementation with named function
function foo(x) {
 r = x++
};
arr.forEach(foo);
pending…
Loop case 1
var i;
for (i = 0; i < arr.length; i += 1) {
  r = arr[i]++
}
pending…
Loop case 2
var i, l;
for (i = 0, l = arr.length; i < l; i += 1) {
  r = arr[i]++
}
pending…
while extract
var i = 0, p

while (p = arr[i++]) {
  r = p++
};
pending…
while extract backwards
var i = arr.length, p

while (p = arr[--i]) {
  r = p++
}
pending…
while extract no cache
var i = 0

while (arr[i++]) {
  r = arr[i]++
}
pending…
while extract back no cache
var i = arr.length

while (arr[--i]) {
  r = arr[i]++
}
pending…
5
var i = arr.length, t = arr.length

while (arr[--i]) {
  r = arr[i]++
}
pending…
6
var i = arr.length, t = arr.length

while (i--) {
  r = arr[t-i-1]++
}
pending…
7
var i = arr.length, t = arr.length

while (i-- > 0) {
  r = arr[t-i-1]++
}
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.

0 Comments