Fastest Array Loops

JavaScript performance comparison

Revision 50 of this test case created by Terry Anastasiadis

Preparation code


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

};
</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
Raw : While, length -= 1
var l = arr.length;
while (l -= 1) {
  //I'm empty!
}
pending…
Raw : For, i += 1
for (var i = 0, l = arr.length; i < l; i += 1) {
  //I'm empty!
}
pending…
Raw : For outter, i += 1
var i = 0,
    l = arr.length;
for (; i < l; i += 1) {
  //I'm empty!
}
pending…
While, length -= 1
var l = arr.length;
while (l -= 1) {
  doStuff(arr[l]);
}
pending…
For, i += 1
for (var i = 0, l = arr.length; i < l; i += 1) {
  doStuff(arr[i]);
}
pending…
For outter, i += 1
var i = 0,
    l = arr.length;
for (; i < l; i += 1) {
  doStuff(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.

0 Comments