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

Revision 87 of this benchmark created on


Description

My each2 plugin

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');

  // 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);
  (function($) {
    $.fn.each2_ref_len_mh = function(fn) {
      var jq = $([]);
      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>

Test runner

Ready to run.

Testing in
TestOps/sec
no elem ref, no explicit length comparison
a.each2(function(i, jq) {
  jq; // jQuery object
});
ready
elem ref, no explicit length comparison
a.each2_ref(function(i, jq) {
  jq; // jQuery object
});
ready
no elem ref, explicit length comparison
a.each2_len(function(i, jq) {
  jq; // jQuery object
});
ready
elem ref, explicit length comparison
a.each2_ref_len(function(i, jq) {
  jq; // jQuery object
});
ready
original jQuery.fn.each (reference)
a.each(function(i, elem) {
  $(this); // jQuery object
});
ready
elem ref, explicit length comparison, inner jq
a.each2_ref_len_mh(function(i, jq) {
  jq; // jQuery object
});
ready

Revisions

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