Editing jQuery.fn.each vs. jQuery.fn.quickEach vs simpler stuff 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/alternative approach would be `quickEach`. Notes: * The callback function is normally slower than `each` so the benefit of `quickEach` often isn't dramatic. * This version allows nesting of calls to `quickEach` (previous versions did not allow 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). * Using `finally` instead of `catch` seems to slow FireFox down. 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'); (function() { var jqx = [], level = -1; jQuery.fn.quickEach = function(c) { var jq = jqx[++level] || (jqx[level] = jQuery([1])); try { var i = -1, len = this.length; while (++i < len && c.call(jq, i, jq[0] = this[i]) !== false); } catch (e) { jq[0] = null; level--; throw e; } jq[0] = null; level--; 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) var jq5; 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) //.css('width', '1px'); }); Test 2 Title Async (check if this is an asynchronous test) Code a.quickEach(function() { this //.css('width', '1px'); // jQuery object }); Test 3 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 //.css('width', '1px'); // jQuery object } Test 4 Title Async (check if this is an asynchronous test) Code var jq = jQuery([1]), i = -1; while (jq[0] = a[++i]) { jq //.css('width', '1px'); // jQuery object } Test 5 Title Async (check if this is an asynchronous test) Code //Re-use jq5 (jQuery constructor has non-trivial cost). if (!jq5) { jq5 = jQuery([1]); } var i = -1; while (jq5[0] = a[++i]) { jq5 //.css('width', '1px'); // jQuery object }