Editing jQuery.fn.each vs. jQuery.fn.quickEach This edit will create a new revision. Your details (optional) Name Email (won’t be displayed; might be used for Gravatar) URL Test case details Title * Published (uncheck if you want to fiddle around before making the page public) Description (in case you feel further explanation is needed)(Markdown syntax is allowed) 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. Notes: * The callback function is normally much slower than `each` so the benefit of `quickerEach` often isn't dramatic. A simple for-loop may be more suitable. * Your callback function MUST NOT call `quickEach` (i.e. nesting). * Your callback function MUST NOT store the value of `this` to a closure variable (I accidentally did once - and it took me over an hour to track the problem down). * Don't be tempted to replace the jQuery `each` method with this. 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. Are you a spammer? (just answer the question) Preparation code Preparation code HTML (this will be inserted in the <body> of a valid HTML5 document in standards mode) (useful when testing DOM operations or including libraries) <script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"> </script> <script> var a = $('<div/>').append(Array(100).join('<a></a>')).children(); // 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> Include JavaScript libraries as follows: <script src="//cdn.ext/library.js"></script> Define setup for all tests (variables, functions, arrays or other objects that will be used in the tests) (runs before each clocked test loop, outside of the timed code region) (e.g. define local test variables, reset global variables, clear canvas, etc.) (see FAQ) Define teardown for all tests (runs after each clocked test loop, outside of the timed code region) (see FAQ) Code snippets to compare Test 1 Title Async (check if this is an asynchronous test) Code var count = 0; a.each(function() { $(this).addClass("fred"); }); Test 2 Title Async (check if this is an asynchronous test) Code a.quickEach(function() { this.addClass("fred"); // jQuery object }); Test 3 Title Async (check if this is an asynchronous test) Code a.quickerEach(function() { this.addClass("fred"); // jQuery object }); Test 4 Title Async (check if this is an asynchronous test) Code //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.