JavaScript Native Loop Performance Comparaison (v9)

Revision 9 of this benchmark created by molokoloco on


Description

Fastest method for looping through an array of 1024 items and do a minimal operation with the new value each time

Some guys of http://parisjs.org on the place ;)

Preparation HTML

<script src="//code.jquery.com/jquery-2.1.0.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.6.0/underscore-min.js"></script>

Setup

// Populate the base array
    var arr = [];
    for (var i = 0; i < 1024; i++) arr[i] = i;

Test runner

Ready to run.

Testing in
TestOps/sec
for()
for (var i = 0; i < arr.length; i++) { arr[i] + 1 }
ready
for() caching
for (var i = 0, length = arr.length; i < length; i++) { arr[i] + 1 }
ready
for() decrement
for (var i = arr.length - 1; i >= 0; i--) { arr[i] + 1 }
ready
for (..in)
for (var key in arr) { arr[key] + 1 }
ready
while() acting like for()
var i = 0;
while (i++ < arr.length) { arr[i - 1] + 1 }
ready
while() caching
var i = 0, length = arr.length;
while (i++ < length) { arr[i - 1] + 1 }
ready
while() in decrement
var length = arr.length;
while (length--) { arr[length] + 1 }
ready
while() in [].pop
var value;
while(value = arr.pop()) { value + 1 }
ready
do while()
var value;
do { value + 1 } while(value = arr.pop())
ready
forEach()
arr.forEach(function(value) { value + 1 })
ready
map()
arr.map(function(value) { return value + 1 })
ready
$.each()
$.each(arr, function(value) { value + 1 })
ready
_.each()
_.each(arr, function(value) { value + 1 })
ready
for (..pop()) - reverse order
for (var value = arr.pop(); value = arr.pop();) { value + 1 }
ready
for (..shift()) - keep order
for (var value = arr.shift(); value = arr.shift();) { value + 1 }
ready
Generator (JS 1.7)
[(arr[key] + 1) for (key in arr)]​
// Fail here but work for me here : 
// http://jsfiddle.net/molokoloco/qeWhB/
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.