jQuery.fn.each vs. jQuery.fn.quickEach

JavaScript performance comparison

Revision 50 of this test case created by private_face

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<script>
  var a = $('<div/>').append(Array(100).join('<a></a>')).children();
  var arr = $.makeArray(a);

  jQuery.fn.betterEach = (function() {
    var jq = jQuery([1]);
    var slice = Array.prototype.slice;
    var el;
    return function(c) {
       var iter = function(e, i) {
         el = jq[0] = e;
         c.call(jq, i, el);
       };
       slice.call(this).forEach(iter);
       jq[0] = 1;
       return this;
    }
  })();

  // Modified quickEach plugin
  jQuery.fn.quickerEach = (function() {
    var jq = jQuery([1]);
    return function(c) {
      var i = -1, el;
      try {
        while (el = jq[0] = this[++i] && c.call(jq, i, el) !== false);
      } catch (e) {
        jq[0] = 1;
        throw e;
      }
      jq[0] = 1;
      return this;
    };
  }());

  // 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;
    };
  }());
</script>
    

Preparation code output

<script> var a = $('<div/>').append(Array(100).join('<a></a>')).children(); var arr = $.makeArray(a); jQuery.fn.betterEach = (function() { var jq = jQuery([1]); var slice = Array.prototype.slice; var el; return function(c) { var iter = function(e, i) { el = jq[0] = e; c.call(jq, i, el); }; slice.call(this).forEach(iter); jq[0] = 1; return this; } })(); // Modified quickEach plugin jQuery.fn.quickerEach = (function() { var jq = jQuery([1]); return function(c) { var i = -1, el; try { while (el = jq[0] = this[++i] && c.call(jq, i, el) !== false); } catch (e) { jq[0] = 1; throw e; } jq[0] = 1; return this; }; }()); // 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; }; }()); </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
.each()
var count = 0;
a.each(function() {
  $(this).addClass("fred");
});
pending…
.quickEach()
a.quickEach(function() {
  this.addClass("fred"); // jQuery object
});
pending…
.quickerEach()
a.quickerEach(function() {
  this.addClass("fred"); // jQuery object
});
pending…
For loop
//Note that this may be more suitable for your needs.
var jq = jQuery([1]);
for (var i = 0, len = a.length; i < len; i++) {
  jq[0] = a[i];
  jq.addClass("fred"); // jQuery object
}
//For fastest performance - avoid jQuery and go direct to the DOM.
pending…
Native forEach
//Note that this may be more suitable for your needs.
var jq = jQuery([1]);
arr.forEach(function(e, i) {
  jq[0] = e;
  jq.addClass("fred"); // jQuery object
});
//For fastest performance - avoid jQuery and go direct to the DOM.
pending…
.betterEach
a.betterEach(function() {
  this.addClass("fred"); // 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