Fastest array loops in Javascript

JavaScript performance comparison

Revision 304 of this test case created by Glen

Preparation code

<script>
  // Populate the base array
  var arr1 = [];
  for (var i = 0; i < 1000; i++) {
    arr1[i] = i;
  }

  function someFn(ix) {
    return ix * 5 + 1 / 3 * 8;
  }
</script>
    

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
For loop, basic
arr = arr1.splice(0);
for (var i = 0; i < arr.length; i++) {
  someFn(arr[i]);
}
pending…
Without braces
arr = arr1.splice(0);
for(var i = arr.length; --i >= 0; someFn(arr[i]));
pending…
For length !== 0
arr = arr1.splice(0);
for(var l = arr.length; l-- !== 0;) {
  someFn(arr[l]);
}
pending…
shift()
arr = arr1.splice(0);
while (i = arr.shift()) {
  someFn(i);
}
pending…
shift while with length test
arr = arr1.splice(0);
while (arr.length) {
  i = arr.shift();
  someFn(i);
}
pending…
While length--
arr = arr1.splice(0);
var l = arr.length;
while(l--) {
  someFn(arr[l]);
}
pending…
For length--
arr = arr1.splice(0);
for(var l = arr.length; l--;) {
  someFn(arr[l]);
}
pending…

Compare results of other browsers

Revisions

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

0 Comments