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

JavaScript performance comparison

Revision 54 of this test case created


Fast alternative to $.fn.each(func). Most usage of fn.each immediately evaluates $(this) - which is really expensive. This version changes this so that it already points to a single-element jQuery object.


My previous implementation (48) was bobbins because modern JS engines use short-circuiting to avoid doing work ... and there was a bug that you could drive a bus through.

Preparation code

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

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

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
var count = 0;
a.each(function() {
a.quickEach(function() {
  this.addClass("fred"); // jQuery object
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.

Compare results of other browsers


You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:


Comment form temporarily disabled.

Add a comment