Fastest array loops in Javascript

JavaScript performance comparison

Revision 73 of this test case created

Preparation code

<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

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
While length--
var l = arr.length;
while (l--) {
  someFn(arr[l]);
}
pending…
For loop, cache
for (var i = 0, len = arr.length; i < len; i++) {
  someFn(arr[i]);
}
pending…
Cla
for (var i = 0, l = arr.length; i !== l; i++) {
  someFn(arr[i]);
}
pending…
For2
var l = arr.length
for (; --l;) {
  someFn(arr[l]);
}
pending…
For3
for (var i = 0, l = arr.length; i < l; i++) {
  someFn(arr[i]);
}
pending…
for4
var i = 0,
  l = arr.length;
for (; i !== l; i++) {
  someFn(arr[i]);
}
pending…
for5
var l = arr.length;
for (; l--;) {
  someFn(arr[l]);
}
pending…
for6
var i = 0,
  l = arr.length;
for (; i < l; i++) {
  someFn(arr[i]);
}
pending…
for7
for (var i = -1, l = arr.length; ++i !== l;) {
  someFn(arr[i]);
}
pending…
for8
for (var i = 0, l = arr.length; i !== l;) {
  someFn(arr[i++]);
}
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. Here’s a list of current revisions for this page:

0 comments

Add a comment