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 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

Ready to run.

Testing in
TestOps/sec
while loop that imitates a for loop
var i = 0;
while (i < arr.length) {
 doNothingWith(arr[i]);
 i++;
};
ready
while loop that imitates a for loop, caching the length
var i = 0,
    len = arr.length;
while (i < len) {
 doNothingWith(arr[i]);
 i++;
};
ready
Reverse while loop
var i = arr.length;
while (i--) {
 doNothingWith(arr[i]);
};
ready
Reverse do … while loop
var i = arr.length;
do {
 doNothingWith(arr[i]);
} while (i--);
ready
Reverse for loop
for (var i = arr.length; i--;) {
 doNothingWith(arr[i]);
};
ready
Old ’n’ busted for loop
for (var i = 0; i < arr.length; ++i) {
 doNothingWith(arr[i]);
};
ready
Old ’n’ busted for loop, caching the length
for (var i = 0, len = arr.length; i < len; ++i) {
 doNothingWith(arr[i]);
};
ready
Cool guy loop
for (var i = -1; ++i < arr.length;) {
 doNothingWith(arr[i]);
};
ready
Cool guy loop, caching the length
for (var i = -1, len = arr.length; ++i < len;) {
 doNothingWith(arr[i]);
};
ready
Native Array#forEach implementation
arr.forEach(function(x) {
 doNothingWith(x);
});
ready
Native Array#forEach implementation with named function
function foo(x) {
 doNothingWith(x);
};
arr.forEach(foo);
ready
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]);
};
ready
Native Array#forEach
arr.forEach(doNothingWith);
ready
fully used for loop via --i
for (var i = arr.length; i; doNothingWith(arr[--i]));
ready
fully used for loop via i--
for (var i = arr.length; i--; doNothingWith(arr[i]));
ready
fully used for loop via --i and call
for (var context = {}, i = arr.length; i; doNothingWith.call(context, arr[--i]));
ready
Native Array#forEach with context
arr.forEach(doNothingWith, {});
ready
safer for loop (aka: forEach logic)
for (var i = arr.length; i--;) {
 if (i in arr) {
  doNothingWith(arr[i]);
 }
}
ready
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]);
 }
}
ready

Revisions

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