Loops (v291)

Revision 291 of this benchmark created by Ludwig Stecher on


Setup

var arr = new Array(300).fill(Math.random());
  var out = new Array(300);

Teardown



            if (out.length !== arr.length) {
    console.error("Test failed")
  }
        
  

Test runner

Ready to run.

Testing in
TestOps/sec
Reverse while loop without implicit ToBoolean
var i = arr.length;
while (i-- > 0) {
 out.push(arr[i]);
}
ready
Native Array#forEach implementation
arr.forEach(function(x) {
 out.push(x);
});
ready
Cool guy loop, caching the length
for (var i = -1, len = arr.length; ++i < len;) {
 out.push(arr[i]);
}
ready
while loop that imitates a for loop
var i = 0;
while (i < arr.length) {
 out.push(arr[i]);
 i++;
}
ready
Cool guy loop
for (var i = -1; ++i < arr.length;) {
 out.push(arr[i]);
}
ready
Native Array#forEach implementation with named function
function foo(x) {
 out.push(x);
}
arr.forEach(foo);
ready
Reverse for loop
for (var i = arr.length; i--;) {
 out.push(arr[i]);
}
ready
Reverse do … while loop
var i = arr.length;
do {
 out.push(arr[i]);
} while (i--);
ready
Reverse while loop
var i = arr.length;
while (i--) {
 out.push(arr[i]);
}
ready
Native Array#forEach with closure
arr.forEach(x => out.push(x));
ready
Old ’n’ busted for loop, caching the length
for (var i = 0, len = arr.length; i < len; ++i) {
 out.push(arr[i]);
}
ready
for of
for (const item of arr) {
 out.push(item);
}
ready
Old ’n’ busted for loop
for (var i = 0; i < arr.length; ++i) {
 out.push(arr[i]);
}
ready
while loop that imitates a for loop, caching the length
var i = 0,
    len = arr.length;
while (i < len) {
 out.push(arr[i]);
 i++;
}
ready

Revisions

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