for vs $.each vs. _.each

JavaScript performance comparison

Revision 14 of this test case created

Info

Testing the time it takes to loop an array using native vs for.

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="//documentcloud.github.com/underscore/underscore-min.js"></script>
<script>
Benchmark.prototype.setup = function() {
    var array = new Array();  
    for (var i=0; i<100000; i++) {  
        array[i] = 0;  
    }
};
</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
Native JS (for)
var l = array.length;  
for (var i=0;i<l; i++) {  
    //array[i] = i;  
}
pending…
$.each
// async test
$.each (array, function (i) {  
    //array[i] = i;  
});
pending…
$(array).each
// async test
$(array).each(function(i) {
    //array[i] = i;
});
pending…
_.each
// async test
_.each(array, function(val, key) {
   //array[key] = key;
});
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