Loops (v127)

Revision 127 of this benchmark created by Gabriel Dibble on


Preparation HTML

<script>
  var arr = [ 120 ];
  var obj = {};
  for( var i = 0, len = arr.length; i < len; i++ ) {
    obj[ i ] = null;
  }

  function advancedLoop( data, callback ) {
    for( var i = 0, j; undefined !== ( j = arr[ i ]); i++ ) {
      callback( j );
    };
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
1) while loop that imitates a for loop
var i = 0;
while (i < arr.length) {
  arr[i];
  i++;
};
ready
2) while loop that imitates a for loop, caching the length
var i = 0,
    len = arr.length;
while (i < len) {
  arr[i];
  i++;
};
ready
3) Reverse while loop
var i = arr.length;
while (i--) {
  arr[i];
};
ready
4) Reverse while loop without implicit ToBoolean
var i = arr.length;
while (i-- > 0) {
  arr[i];
};
ready
5) Reverse do … while loop
var i = arr.length;
do {
  arr[i];
} while (i--);
ready
6) Reverse for loop
for (var i = arr.length; i--;) {
  arr[i];
};
ready
7) Old ’n’ busted for loop
for (var i = 0; i < arr.length; ++i) {
  arr[i];
};
ready
8) Old ’n’ busted for loop, caching the length
for (var i = 0, len = arr.length; i < len; ++i) {
  arr[i];
};
ready
9) Cool guy loop
for (var i = -1; ++i < arr.length;) {
  arr[i];
};
ready
10) Cool guy loop, caching the length
for (var i = -1, len = arr.length; ++i < len;) {
  arr[i];
};
ready
11) Native Array#forEach implementation
arr.forEach(function(x) {
  x;
});
ready
12) Native Array#forEach implementation with named function
function foo(x) {
  x;
};
arr.forEach(foo);
ready
13) For In Array
for (i in arr) {
  arr[i]
}
ready
14) For In Object
for (i in obj) {
  obj[i]
}
ready
15) For loop with assignation as condition
for (var i = 0, j; undefined !== (j = arr[i]); i++) {
  j;
};
ready
16) For loop with assignation as condition and with callback
advancedLoop(arr, function(i) {
  i;
});
ready
17) Old ’n’ busted for loop, var outside loop
var len = arr.length;
for( var i = 0; i < len; ++i ) {
  arr[ i ];
};
ready
18) Reverse for loop, var outside loop
var len = arr.length;
for( var i = len; i--; ) {
  arr[ i ];
};
ready
19) 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.