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) [The `quickEach` method](http://gist.github.com/500145) will pass a non-unique `jQuery` instance to the callback meaning that there will be no need to instantiate a fresh `jQuery` instance on each iteration. Most of the slow-down inherent in jQuery’s native iterator method (`each`) is the constant need to have access to jQuery’s methods, and so most developers see constructing multiple instances as no issue… A better approach would be `quickEach`. My previous test case was bobbins - so we're back to James Padolsey's original quickEach - and the fact that in many scenarios it is about the same speed as a simple while loop. Have added "Cowboy" Ben Alman's version for comparison. 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>')).find('a'); //Original version by James Padolsey. //The try/catch and delete were designed to ensure no memory leak. 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; }; }()); /*! * jQuery each2 - v0.2 - 8/02/2010 * http://benalman.com/projects/jquery-misc-plugins/ * * Inspired by James Padolsey's quickEach * http://gist.github.com/500145 * * Copyright (c) 2010 "Cowboy" Ben Alman * Dual licensed under the MIT and GPL licenses. * http://benalman.com/about/license/ */ (function($) { // Create a placeholder jQuery object with a length of 1. The single item // is completely arbitrary and will be replaced. var jq = $([1]); $.fn.each2 = function(fn) { var i = -1; while ( // Set both the first element AND context property of the placeholder // jQuery object to the DOM element. When i has been incremented past the // end, this[++i] will return undefined and abort the while loop. (jq.context = jq[0] = this[++i]) // Invoke the callback function in the context of the DOM element, // passing both the index and the placeholder jQuery object in. Like // .each, if the callback returns `false`, abort the while loop. && fn.call(jq[0], i, jq) !== false) {} // Return the initial jQuery object for chainability. return this; }; })(jQuery); /* * (cc:by) Artūrs Jansons */ jQuery.fn.each3 = (function() { return function(c) { for(x in this){ !(++x) || c.call(this,x,this[x]) !== false } 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 a.each(function() { $(this).addClass("wibble"); }); Test 2 Title Async (check if this is an asynchronous test) Code //Implementation by James Padolsey. a.quickEach(function() { this.addClass("wibble"); // jQuery object }); Test 3 Title Async (check if this is an asynchronous test) Code //Implemented by "Cowboy" Ben Alman. a.each2(function(i, jq) { jq.addClass("wibble"); // jQuery object }); Test 4 Title Async (check if this is an asynchronous test) Code //$([DomElement]) is faster than $([1]) - we are going to discard //jq variable after loop has finished so possibility of zero-length //is not important. //Please, cache length. var jq = jQuery(a[0]); for (var i = 0, l = a.length; i < l; i+=1) { jq[0] = a[i]; jq.addClass("wibble"); } Test 5 Title Async (check if this is an asynchronous test) Code //What's the cost of instantiating jQuery object? //About 25% when you have addClass() inside the loop. for (var i = 0, l = a.length; i < l; i++) { var jq = $(a[i]); jq.addClass("wibble"); } Test 6 Title Async (check if this is an asynchronous test) Code var jq = jQuery(a[0]), i = -1; while (jq[0] = a[++i]) { jq.addClass("wibble"); } Test 7 Title Async (check if this is an asynchronous test) Code var jq = jQuery(a[0]) for (x in jq) { jq.addClass("wibble"); } Test 8 Title Async (check if this is an asynchronous test) Code a.each3(function(){ $(this).addClass("wibble"); })