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.quickEach = (function() { return function(arr, c) { var i = -1, el, len = arr.length; try { while (++i < len && (el = arr[i]) && c.call(el, i, el) !== false); } catch (e) { throw e; } }; }()); </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) no string concat Code snippets to compare Test 1 Title Async (check if this is an asynchronous test) Code var sum = 0; var i = 0; var length = array.length; for (i; i < length; i++) { item = array[i]; sum += item; } Test 2 Title Async (check if this is an asynchronous test) Code var sum = 0; _.each(array, function(item) { return sum += item; }); Test 3 Title Async (check if this is an asynchronous test) Code var sum = 0; $.each(array, function() { return sum += this; }); Test 4 Title Async (check if this is an asynchronous test) Code var sum = 0; array.forEach(function(item) { return sum += item; }); Test 5 Title Async (check if this is an asynchronous test) Code var sum = 0; $.quickEach(array, function() { return sum += this; }); Test 6 Title Async (check if this is an asynchronous test) Code var sum = 0; var i = 0; var length = array.length; for (i; i < length; i++) { (function(i) { var item = array[i]; sum += item; })(i); }