Editing CoffeeScript's for vs. Underscore _.each vs. jQuery $.each 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) Compare for loop performance. Added QuickEach by James Padolsey : http://jsperf.com/jquery-each-vs-quickeach 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="https://raw.github.com/documentcloud/underscore/ba3e31b53ef5752b40cfb2d71f594536fefbe916/underscore-min.js"> </script> <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"> </script> <script> jQuery.fn.quickEach = jQuery.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) var _i, _results; window.array = (function() { _results = []; for (_i = 1; _i <= 1000; _i++) { _results.push(_i); } return _results; }).apply(this); 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 /* string = "" for item in array string += item.toString() */ var item, string, _i, _len; string = ""; for (_i = 0, _len = array.length; _i < _len; _i++) { item = array[_i]; string += item.toString(); } Test 2 Title Async (check if this is an asynchronous test) Code var string = ""; _.each(array, function(item) { return string += item.toString(); }); Test 3 Title Async (check if this is an asynchronous test) Code var string = ""; $.each(array, function() { return string += this.toString(); }); Test 4 Title Async (check if this is an asynchronous test) Code var string = ""; array.forEach(function(item) { return string += item.toString(); }); Test 5 Title Async (check if this is an asynchronous test) Code var string = ""; $(array).quickEach(function() { return string += this[0].toString(); }); Test 6 Title Async (check if this is an asynchronous test) Code var string = ""; var len = array.length; var i = 0; while (i < len) { string += array[i].toString(); i++; }