jQuery.fn.each vs. quickEach vs. each2

JavaScript performance comparison

Revision 2 of this test case created by "Cowboy" Ben Alman

Info

James Padolsey's quickEach plugin
My each2 plugin (which is like, almost the same as James')

Based on http://jsperf.com/jquery-each-vs-quickeach.

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script>
  var a = $('<div/>').append(Array(100).join('<a></a>')).find('a');
 
  // James Padolsey's quickEach plugin
  jQuery.fn.quickEach = (function() {
   var jq = jQuery([1]);
   return function(c) {
    var i = -1,
        el, len = this.length;
    try {
     while (++i < len && (el = jq[0] = this[i]) && c.call(jq, i, el) !== false);
    } catch (e) {
     delete jq[0];
     throw e;
    }
    delete jq[0];
    return this;
   };
  }());
 
  // My each2 plugin
  (function($) {
   var jq = $([]);
   $.fn.each2 = function(fn) {
    var i = -1,
        elem;
    while ((elem = jq[0] = this[++i]) && fn.call(elem, i, jq) !== false) {}
    return this;
   };
  })(jQuery);
</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
.each with no jQuery object
a.each(function(i, elem) {
 this; // dom element
});
pending…
.each2 with no jQuery object
a.each2(function(i, jq) {
 this; // dom element
});
pending…
.quickEach with no jQuery object
a.quickEach(function(i, elem) {
 elem; // dom element
});
pending…
.each with jQuery object
a.each(function(i, elem) {
 $(this); // jQuery object
});
pending…
.each2 with jQuery object
a.each2(function(i, jq) {
 jq; // jQuery object
});
pending…
.quickEach with jQuery object
a.quickEach(function(i, elem) {
 this; // jQuery object
});
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