Fastest array loops in Javascript (v73)

Revision 73 of this benchmark created on


Preparation HTML

<script>
  // Populate the base array
  var arr = [];
  for (var i = 0; i < 1000; i++) {
    arr[i] = i;
  }
  function someFn(ix) {
    console.log(ix);
  }

</script>

Test runner

Ready to run.

Testing in
TestOps/sec
While length--
var l = arr.length;
while (l--) {
  someFn(arr[l]);
}
ready
For loop, cache
for (var i = 0, len = arr.length; i < len; i++) {
  someFn(arr[i]);
}
ready
Cla
for (var i = 0, l = arr.length; i !== l; i++) {
  someFn(arr[i]);
}
ready
For2
var l = arr.length
for (; --l;) {
  someFn(arr[l]);
}
ready
For3
for (var i = 0, l = arr.length; i < l; i++) {
  someFn(arr[i]);
}
ready
for4
var i = 0,
  l = arr.length;
for (; i !== l; i++) {
  someFn(arr[i]);
}
ready
for5
var l = arr.length;
for (; l--;) {
  someFn(arr[l]);
}
ready
for6
var i = 0,
  l = arr.length;
for (; i < l; i++) {
  someFn(arr[i]);
}
ready
for7
for (var i = -1, l = arr.length; ++i !== l;) {
  someFn(arr[i]);
}
ready
for8
for (var i = 0, l = arr.length; i !== l;) {
  someFn(arr[i++]);
}
ready

Revisions

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