jQuery.fn.each vs. quickEach vs. each2

JavaScript performance comparison

Revision 92 of this test case created

Info

My each2 plugin

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

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div id="test"></div>
<script>
  var a = [1,2,3,4,5];
 
  // My each2 plugin
  (function($) {
   var jq = $([]);
   $.fn.each2 = function(fn) {
    var i = -1;
    while ((jq[0] = this[++i]) && fn.call(jq[0], i, jq) !== false) {}
    return this;
   };
  })(jQuery);
 
  (function($) {
   var jq = $([]);
   $.fn.each2_ref = function(fn) {
    var i = -1,
        elem;
    while ((elem = jq[0] = this[++i]) && fn.call(elem, i, jq) !== false) {}
    return this;
   };
  })(jQuery);
 
  (function($) {
   var jq = $([]);
   $.fn.each2_len = function(fn) {
    var i = -1,
        len = this.length;
    while (++i < len && (jq[0] = this[i]) && fn.call(jq[0], i, jq) !== false) {}
    delete jq[0];
    return this;
   };
  })(jQuery);
 
  (function($) {
   var jq = $([]);
   $.fn.each2_ref_len = function(fn) {
    var i = -1,
        len = this.length,
        elem;
    while (++i < len && (elem = jq[0] = this[i]) && fn.call(elem, i, jq) !== false) {}
    delete jq[0];
    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
no elem ref, no explicit length comparison
a.each2(function(i, jq) {
  $('#test').append(jq); // jQuery object
});
pending…
elem ref, no explicit length comparison
a.each2_ref(function(i, jq) {
  $('#test').append(jq); // jQuery object
});
pending…
no elem ref, explicit length comparison
a.each2_len(function(i, jq) {
  $('#test').append(jq); // jQuery object
});
pending…
elem ref, explicit length comparison
a.each2_ref_len(function(i, jq) {
  $('#test').append(jq); // jQuery object
});
pending…
original jQuery.fn.each (reference)
a.each(function(i, elem) {
  $('#test').append($(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