Fastest array loops in Javascript (v378)

Revision 378 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) {
    return ix * 5 + 1 / 3 * 8;
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
For loop, basic
var l =  arr.length;
for (var i = 0; i < l; i++) {
  someFn(arr[i]);
}
ready
While loop, basic
var i = 0;
var l = arr.length;
while (i++ < l) {
  someFn(arr[i]);
}
ready
Order
while( i = arr.shift() ) {
  someFn(i);
}
ready
order validation
while( (i = arr.shift()) !== undefined ) {
  someFn(i);
}
ready
While ! Undefined
var a;
while((a = arr[i++]) !== undefined) {
  someFn(a);
}
ready
Pop
while( i = arr.pop() ) {
  someFn(i);
}
ready
Order w/ array copy
var copy = arr.slice(0);
while( i = copy.shift() ) {
  someFn(i);
}
ready
While length--
var l = arr.length;
while(l--) {
  someFn(arr[l]);
}
ready
123213
var n = arr.length;
var l = -n;
while(l++) {
  someFn(arr[l+n]);
}
ready

Revisions

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