Loops (long array) (more accurate tests)

JavaScript performance comparison

Revision 6 of this test case created by WebReflection and last updated

Preparation code

<script>
  var arr = [1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3, 1, 2, 3];
  
  
  function doNothingWith(something) {
   return something
  }
</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) {
 doNothingWith(arr[i]);
 i++;
};
pending…
while loop that imitates a for loop, caching the length
var i = 0,
    len = arr.length;
while (i < len) {
 doNothingWith(arr[i]);
 i++;
};
pending…
Reverse while loop
var i = arr.length;
while (i--) {
 doNothingWith(arr[i]);
};
pending…
Reverse do … while loop
var i = arr.length;
do {
 doNothingWith(arr[i]);
} while (i--);
pending…
Reverse for loop
for (var i = arr.length; i--;) {
 doNothingWith(arr[i]);
};
pending…
Old ’n’ busted for loop
for (var i = 0; i < arr.length; ++i) {
 doNothingWith(arr[i]);
};
pending…
Old ’n’ busted for loop, caching the length
for (var i = 0, len = arr.length; i < len; ++i) {
 doNothingWith(arr[i]);
};
pending…
Cool guy loop
for (var i = -1; ++i < arr.length;) {
 doNothingWith(arr[i]);
};
pending…
Cool guy loop, caching the length
for (var i = -1, len = arr.length; ++i < len;) {
 doNothingWith(arr[i]);
};
pending…
Native Array#forEach implementation
arr.forEach(function(x) {
 doNothingWith(x);
});
pending…
Native Array#forEach implementation with named function
function foo(x) {
 doNothingWith(x);
};
arr.forEach(foo);
pending…
Unrolled loop
for (var i = 0, len = arr.length; i < len; i = i + 10) {
 doNothingWith(arr[i]);
 doNothingWith(arr[i + 1]);
 doNothingWith(arr[i + 2]);
 doNothingWith(arr[i + 3]);
 doNothingWith(arr[i + 4]);
 doNothingWith(arr[i + 5]);
 doNothingWith(arr[i + 6]);
 doNothingWith(arr[i + 7]);
 doNothingWith(arr[i + 8]);
 doNothingWith(arr[i + 9]);
};
pending…
Native Array#forEach
arr.forEach(doNothingWith);
pending…
fully used for loop via --i
for (var i = arr.length; i; doNothingWith(arr[--i]));
pending…
fully used for loop via i--
for (var i = arr.length; i--; doNothingWith(arr[i]));
pending…
fully used for loop via --i and call
for (var context = {}, i = arr.length; i; doNothingWith.call(context, arr[--i]));
pending…
Native Array#forEach with context
arr.forEach(doNothingWith, {});
pending…
safer for loop (aka: forEach logic)
for (var i = arr.length; i--;) {
 if (i in arr) {
  doNothingWith(arr[i]);
 }
}
pending…
even safer for loop (aka: better forEach logic)
// the precedent loop may fail via
// Object.prototype["0"] = 0;
// this one better respects forEach logic
for (var i = arr.length; i--;) {
 if (arr.hasOwnProperty(i)) {
  doNothingWith(arr[i]);
 }
}
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