Editing jQuery.each vs. for loop vs Ext.Array.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) Serious Test for common method for looping an array: - (native) classic for loop - (native) `Array.forEach` - `.each` from various frameworks: jQuery, Ext, underscore (original & fallback version) - emulation: `Array.prototype.each` Array size: 1000 (array size for testing the true processing power, unusual but possible) 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 src="http://cdn.sencha.io/ext-4.1.0-gpl/ext-all.js"> </script> <script src="https://raw.github.com/documentcloud/underscore/master/underscore.js"> </script> <script> $.fn.forEach = Array.prototype.forEach; var extArrayEach = Ext.Array.each; Array.prototype.each = function(func){ var array = this, i = 0, l = array.length; while(i < l) func(array[i], i, array), i++; return array }; var breaker = {}; _.forEach = function(obj, iterator, context) { if (obj == null) return; if (obj.length === +obj.length) { for (var i = 0, l = obj.length; i < l; i++) { if (iterator.call(context, obj[i], i, obj) === breaker) return; } } else { for (var key in obj) { if (_.has(obj, key)) { if (iterator.call(context, obj[key], key, obj) === breaker) return; } } } }; //a static array. var a = []; for (var i = 0, len = 1000; i < len; i++) { a[i] = i; } function makeid() { var text = ""; var possible = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789"; for( var i=0; i < 5; i++ ) text += possible.charAt(Math.floor(Math.random() * possible.length)); return text; } var b = {}; for (var i = 0, len = 1000; i < len; i++) { b[makeid()] = i; } </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 $.each(a, function(index, node) { var e = node; }); Test 2 Title Async (check if this is an asynchronous test) Code for (var i = 0, len = a.length; i < len; i++) { var e = a[i]; }; Test 3 Title Async (check if this is an asynchronous test) Code a.forEach(function(node, index){ var e = node; }); Test 4 Title Async (check if this is an asynchronous test) Code extArrayEach(a, function(index, node) { var e = node; }); Test 5 Title Async (check if this is an asynchronous test) Code a.each(function(node){ var e = node; }) Test 6 Title Async (check if this is an asynchronous test) Code _.each(a, function(node, index){ var e = node; }) Test 7 Title Async (check if this is an asynchronous test) Code _.forEach(a, function(node, index){ var e = node; }) Test 8 Title Async (check if this is an asynchronous test) Code $.each(b, function(index, node) { var e = node; }); Test 9 Title Async (check if this is an asynchronous test) Code for(var index in b) { var node = b[index]; }