js loops

JavaScript performance comparison

Revision 12 of this test case created

Info

Checking the real performance difference for the various javascript loops patterns.

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var bigArray = [];
    var j = 0;
    for (var i = 0; i < 1000; i++) {
      bigArray.push(i+1);
    }
};
</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
Case 1
for (var i = 0; i < bigArray.length; i++) {
  bigArray[i]++;
}
pending…
Case 2
for (var i = bigArray.length; i--;) {
  bigArray[i]++;
}
pending…
Case 3
for (var i = 0; i < bigArray.length; ++i) {
  bigArray[i]++;
}
pending…
Cached Case 1
var length = bigArray.length;
for (var i = 0; i < length; i++) {
  bigArray[i]++;
}
pending…
Case 4
var length = bigArray.length;
while(length) {
  bigArray[--length]++;
}
pending…
Case 5
for (var i = 0, val; val=bigArray[i]; i++) {
  val++;
}
pending…
forEach
bigArray.forEach(function(item) {
  item++
})
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