jQuery each vs each2 vs quickeach vs (v45)

Revision 45 of this benchmark created by KergeKacsa at Index.hu on


Preparation HTML

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="https://rawgit.com/cowboy/jquery-misc/master/jquery.ba-each2.js"></script>
<script>
//quickEach
  (function($) {
    $.fn.quickEach = function(f) {
      var j = $([0]),
          i = -1,
          l = this.length,
          c;
      while (++i < l && (c = j[0] = this[i]) && f.call(j, i, c) !== false);
      return this;
    };
  })(jQuery);
  // Create a whole bunch of elements for iteration.
  var elems = $('<div/>').append(Array(1000).join('<span/>')).children();
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
jQuery core .each method
elems.each(function(i, elem) {
  var jq = $(this); // jQuery object
});
ready
jQuery each2 plugin
elems.each2(function(i, jq) {
  jq; // jQuery object
});
ready
jQuery quickEach plugin
elems.quickEach(function(i, elem) {
  this; // jQuery object
});
ready
raw for-loop... with $()[..] replacement
// inspired by the each2 plugin itself, admittedly much uglier raw for-loop... but fast
var jq = $([1]); // dummy jquery object to use
for (var i = 0, len = elems.length; i < len; i++) {
  jq.context = jq[0] = elems[i];
  jq; // jQuery object
}
ready

Revisions

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