JavaScript Native Loop Performance Comparaison (v7)

Revision 7 of this benchmark created by molokoloco on


Description

Fastest method for looping through an array of 256 items and do a calcul 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 < 256; 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 i in arr) { arr[i] + 1 }
ready
while() acting like for()
var i = 0;
while (i++ <= arr.length) { arr[i] + 1 }
ready
while() caching
var i = 0, length = arr.length;
while (i++ < length) { arr[i] + 1 }
ready
while() in decrement
var length = arr.length;
while (length--) { arr[length] + 1 }
ready
while() in [].pop
var i;
while(i = arr.pop()) { i + 1 }
ready
do while()
do { i + 1 } while(i = arr.pop())
ready
forEach()
arr.forEach(function(i) { i + 1 })
ready
map()
arr.map(function(i) { i + 1 })
ready
$.each()
$.each(arr, function(i) { i + 1 })
ready
_.each()
_.each(arr, function(i) { i + 1 })
ready

Revisions

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