jQuery each vs each2 vs quickeach vs

JavaScript performance comparison

Revision 45 of this test case created by KergeKacsa at Index.hu

Preparation code

<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>
    

Preparation code output

<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

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
jQuery core .each method
elems.each(function(i, elem) {
  var jq = $(this); // jQuery object
});
pending…
jQuery each2 plugin
elems.each2(function(i, jq) {
  jq; // jQuery object
});
pending…
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
}
pending…
jQuery quickEach plugin
elems.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.

0 Comments