FOR LOOPS test

JavaScript performance comparison

Revision 3 of this test case created

Info

Difference between for/in, classical for loop and classical loop with caching.

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.2/underscore-min.js"></script>
 
<script>
Benchmark.prototype.setup = function() {
    var myArray = [];
    for(var i = 0; i < 1024; i++) {
    myArray.push(i);
    }
};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
for/in loop
for (var i in myArray) {
myArray[i] = myArray[i]++;
}
pending…
for loop (no caching)
for (var i = 0; i < myArray.length; i++) {
myArray[i] = myArray[i]++;
}
pending…
for loop (with caching)
for (var i = 0, l = myArray.length; i < l; i++) {
myArray[i] = myArray[i]++;
}
pending…
jQuery.each (for comparison)
jQuery.map(myArray, function(elem, i) {
return elem++;
});
pending…
for loop (no caching) - caching length
var length = myArray.length;
for (var i = 0; i < length; i++) {
myArray[i] = myArray[i]++;
}
pending…
for loop (with caching) - caching length
var length = myArray.length;
for (var i = 0, l = length; i < l; i++) {
myArray[i] = myArray[i]++;
}
pending…
forEach javascript
myArray.map(function(elem, i) {
return elem++;
});
pending…
underscore
_.map(myArray, function(elem, i) {
return elem++;
});
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