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

JavaScript performance comparison

Test case created by James Padolsey

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></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;

Preparation code output

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
a.quickEach(function() {
 this; // jQuery object
a.each(function() {

Compare results of other browsers


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


Dave Stein commented :

This plug-in only works for arrays since objects don't have a .length function. This just wraps a function around a for loop.

Domenic Denicola commented :

This fails horribly when you try to capturing this in a variable for use in a closure that is not immediately executed, since this gets dynamically updated each loop-through:


Jeff Tucker commented :

@Domenic I think there was just a typo in the jsfiddle example. var $this = $(this); seems to work well.


Will Earp commented :

I like the idea of quickEach(), I am not totally sure how it works but I have found a problem with it. You cannot use another quickEach() inside a quickEach() loop.

When I get some time I will try and understand what it is doing and come up with a fix, but look forward to a solution if you think of one.