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

JavaScript performance comparison

Revision 107 of this test case created by Andy Harman

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>')).find('a');
  
  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>')).find('a'); 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()
a.each(function() {
  $(this).css("background", "red"); // jQuery object
});
pending…
.quickEach()
a.quickEach(function() {
  this.css("background", "red"); // jQuery object
});
pending…
native for loop
for (var i = 0, arr = a.length; i < arr; i++) {
  $(a[i]).css("background", "red");
}
pending…
native for loop with cached reference
//Reference to a single element jQuery collection.
var $x=$(0);

//Iterate thru list, substituting the current DOM element into
//out cached jQuery instance.
for (var i = 0; $x[0]=a[i]; i++) {
  $x.css("background", "red"); // 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