Fastest Array Loops

JavaScript performance comparison

Revision 50 of this test case created by Terry Anastasiadis

Info

Testing loop performance.

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 unknown unknown
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. Here’s a list of current revisions for this page:

0 comments

Add a comment