Looping in Javascript

JavaScript performance comparison

Revision 59 of this test case created by Suraj Reddy

Info

Testing different types of loops in javascript, assuming you need a reference to each variable.

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="//underscorejs.org/underscore-min.js"></script>
 
<script>
Benchmark.prototype.setup = function() {
    arr = [{'a':1}, 10, ['sub-array', 2], 9, {'a':1}, 19238, 1248, 8, {'a':1}, 0, 2, -1, 'string'];
   
};
</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
JQuery
$.each(arr, function(idx, val) {
    // do nothing.
});
 
pending…
for
for (var i = 0; i < arr.length; i += 1) {
    var val = arr[i];
    // Do nothing else.
}
pending…
_.each
_.each(arr, function(idx, val) {
    // Do nothing.
});
pending…
forEach
arr.forEach(function(val, idx) {
    // Do nothing.
})
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