Looping in Javascript (v59)

Revision 59 of this benchmark created by Suraj Reddy on


Description

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

Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="//underscorejs.org/underscore-min.js"></script>

Setup

arr = [{'a':1}, 10, ['sub-array', 2], 9, {'a':1}, 19238, 1248, 8, {'a':1}, 0, 2, -1, 'string'];

Test runner

Ready to run.

Testing in
TestOps/sec
JQuery
$.each(arr, function(idx, val) {
    // do nothing.
});
 
ready
for
for (var i = 0; i < arr.length; i += 1) {
    var val = arr[i];
    // Do nothing else.
}
ready
_.each
_.each(arr, function(idx, val) {
    // Do nothing.
});
ready
forEach
arr.forEach(function(val, idx) {
    // Do nothing.
})
ready

Revisions

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