Underscore.each vs jQuery.each vs. for loop

JavaScript performance comparison

Revision 202 of this test case created

Info

updated to remove the possibility that things are just getting optimized to oblivion.

Preparation code

<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 = new Array(10000),
      e;
  for (var i = 0; i < a.length; i++) a[i] = i;
</script>

<script>
  function m(items, fn) {
    for(var i = 0, length = items.length; i < length; i++) {
      fn(items[i]);
    }
  }
</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
$.each(a, function() {
 e = pi + this;
});
pending…
good old for loop
for (var i = 0, len = a.length; i < len; i++) {
 e = pi + a[i];
};
pending…
underscore.each
_.each(a, function(item) {
 e = pi + item;
});
pending…
micro
m(a, function(item) {
  e = pi + item;
});
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