Underscore.each vs jQuery.each vs. for loop vs. forEach (v273)

Revision 273 of this benchmark created on


Preparation HTML

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script src="//documentcloud.github.com/underscore/underscore-min.js">
</script>
<script>
  var pi = Math.PI
  var a = [], sum;
  for (var i = 0; i < 100000; i++) {
    a.push(Math.floor(Math.random() * 100000));
  }
</script>

Setup

sum = 0

Test runner

Ready to run.

Testing in
TestOps/sec
jQuery.each
$.each(a, function(idx, val) {
  sum += val;
});
ready
good old for loop
for (var i = 0, len = a.length; i < len; i++) {
  sum += a[i];
};
ready
underscore.each
_.each(a, function(val) {
  sum += val
});
ready
good old for loop (decrementing)
var i = a.length;
while (i--) {
  sum += a[i]
}
ready
forEach
a.forEach(function(val) {
  sum += val;
})
ready
map?
a.map(function(val) {
  sum += val
})
ready
for function
var i = a.length;
for (; i > 0; i--) {
  sum += a[i];
};
ready
dumb for loop
for (var i = 0; i < a.length; i++) {
  sum += a[i];
};
ready

Revisions

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