Loops (v128)

Revision 128 of this benchmark created by ASM on


Description

Array size = 40; Removed some obscure cases which were not faster on any of the platforms

Setup

var arr = [];
    var obj = {};
    for( var i = 0; i < 40; i++ ) {
        arr[ i ] = i+1;
        obj[ i ] = i+1;
    }
    
    // to let IE7 && other old browsers run this as well
    if (!Array.prototype.forEach)
    {
      Array.prototype.forEach = function(fun)
      {
        var len = this.length;
        for (var i = 0; i < len; ++i)
            fun(this[i]);
      };
    }

Test runner

Ready to run.

Testing in
TestOps/sec
01) Old ’n’ busted for
for (var i = 0; i < arr.length; ++i) {
  arr[i];
};
ready
02) Old ’n’ busted + length caching
for (var i = 0, len = arr.length; i < len; ++i) {
  arr[i];
};
ready
03) Old ’n’ busted + var outside loop
var len = arr.length;
for (var i = 0; i < len; ++i ) {
  arr[ i ];
};
ready
04) Reverse for
for (var i = arr.length; i--;) {
  arr[i];
};
ready
05) while loop
var i = 0;
while (i < arr.length) {
  arr[i];
  i++;
};
ready
06) while loop + length caching
var i = 0,
  len = arr.length;
while (i < len) {
  arr[i];
  i++;
};
ready
07) Reverse while
var i = arr.length;
while (i--) {
  arr[i];
};
ready
08) Reverse do … while loop
var i = arr.length;
do {
  arr[i];
} while (i--);
ready
09) Cool guy loop + length caching
for (var i = -1, len = arr.length; ++i < len;) {
  arr[i];
};
ready
10) Native forEach
arr.forEach(function(x) {
  x;
});
ready
11) Native forEach with named function
function foo(x) {
  x;
};
arr.forEach(foo);
ready
12) For In Array
for (i in arr) {
  arr[i];
};
ready
13) For In Object
for (i in obj) {
  obj[i];
};
ready
14) Object Property Accessor using Bracket notation
for( var i = 0; arr[i]; i++) {
  arr[i];
};
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.