js loops

JavaScript performance comparison

Test case created by Arash Milani

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);
    }
};
</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++){
    j++;
}
pending…
Case 2
for(var i= bigArray.length; i--;){
    j++;
}
pending…
Case 3
for(var i=0; i < bigArray.length; ++i){
    j++;
}
pending…
Cached Case 1
var length = bigArray.length;
for(var i=0; i < length; i++){
    j++;
}
pending…
Case 4
var length = bigArray.length;
while(length--){
    j++;
}
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:

3 comments

kave commented :

xxxx

michael commented :

Why Case 1 (not cashed) is faster then cashed case? I do not understand it.

michael commented :

I mean FF 18

Add a comment