jQuery.fn.each vs. quickEach vs. each2 (v2)

Revision 2 of this benchmark created by "Cowboy" Ben Alman on


Description

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 HTML

<script src="https://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>

Test runner

Ready to run.

Testing in
TestOps/sec
.each with no jQuery object
a.each(function(i, elem) {
 this; // dom element
});
ready
.each2 with no jQuery object
a.each2(function(i, jq) {
 this; // dom element
});
ready
.quickEach with no jQuery object
a.quickEach(function(i, elem) {
 elem; // dom element
});
ready
.each with jQuery object
a.each(function(i, elem) {
 $(this); // jQuery object
});
ready
.each2 with jQuery object
a.each2(function(i, jq) {
 jq; // jQuery object
});
ready
.quickEach with jQuery object
a.quickEach(function(i, elem) {
 this; // jQuery object
});
ready

Revisions

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