Fastest Array Loops (v50)

Revision 50 of this benchmark created by Terry Anastasiadis on


Setup

var arr = [];
  
  for (var i = 0; i < 1000; i += 1) {
    arr.push(i);
  }
  
  function doStuff(x) {
    return (x + x);
  }

Test runner

Ready to run.

Testing in
TestOps/sec
Raw : While, length -= 1
var l = arr.length;
while (l -= 1) {
  //I'm empty!
}
ready
Raw : For, i += 1
for (var i = 0, l = arr.length; i < l; i += 1) {
  //I'm empty!
}
ready
Raw : For outter, i += 1
var i = 0,
    l = arr.length;
for (; i < l; i += 1) {
  //I'm empty!
}
ready
While, length -= 1
var l = arr.length;
while (l -= 1) {
  doStuff(arr[l]);
}
ready
For, i += 1
for (var i = 0, l = arr.length; i < l; i += 1) {
  doStuff(arr[i]);
}
ready
For outter, i += 1
var i = 0,
    l = arr.length;
for (; i < l; i += 1) {
  doStuff(arr[i]);
}
ready

Revisions

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