Editing Loops 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) Compares speed of variations of the for, for-in, while, do-while, and Array.forEach looping constructs. Changes since revision 22 : Fixed the "Reverse `do … while` loop" test so it doesn't make the arr array larger for later tests. : Normalized operation complexity to arr[i] *= 1 for all tests so later iteration of tests aren't doing calculations with larger values (i.e. 1998 * 5 => 9,990 * 5 => 49,950). : Fixed the "for .. in (will skip empty values)" test so it only uses arr's elements and excludes standard and injected Array members. : Fixed the Preparation code so it doesn't leak its local "k" loop counter variable into the global scope. 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> var arr = []; (function populateWithoutPollutingGlobalScope () { var k = 2000; while (k--) { arr[k]=k; } })(); // Intentionally pollute all objects for the for-in test(s): Object.prototype[Number.MIN_VALUE] = "Externally polluted."; </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 var i = 0; while (i < arr.length) { arr[i] *= 1; i++; } Test 2 Title Async (check if this is an asynchronous test) Code var i = 0 , len = arr.length ; while (i < len) { arr[i] *= 1; i++; } Test 3 Title Async (check if this is an asynchronous test) Code var i = arr.length; while (i--) { arr[i] *= 1; } Test 4 Title Async (check if this is an asynchronous test) Code var i = arr.length; while (i-- > 0) { arr[i] *= 1; } Test 5 Title Async (check if this is an asynchronous test) Code var i = arr.length - 1; do { arr[i] *= 1; } while (i--); Test 6 Title Async (check if this is an asynchronous test) Code for (var i = arr.length; i--;) { arr[i] *= 1; } Test 7 Title Async (check if this is an asynchronous test) Code for (var i = 0; i < arr.length; ++i) { arr[i] *= 1; } Test 8 Title Async (check if this is an asynchronous test) Code for (var i = 0, len = arr.length; i < len; ++i) { arr[i] *= 1; } Test 9 Title Async (check if this is an asynchronous test) Code for (var i = -1; ++i < arr.length;) { arr[i] *= 1; } Test 10 Title Async (check if this is an asynchronous test) Code for (var i = -1, len = arr.length; ++i < len;) { arr[i] *= 1; } Test 11 Title Async (check if this is an asynchronous test) Code arr.forEach(function(x) { x *= 1; }); Test 12 Title Async (check if this is an asynchronous test) Code function foo(x) { x *= 1; } arr.forEach(foo); Test 13 Title Async (check if this is an asynchronous test) Code var i; for (i in arr) { if (arr.hasOwnProperty (i) && isFinite (i)) { // Assured use of arr's elements vs. members like "length". arr[i] *= 1; } } Test 14 Title Async (check if this is an asynchronous test) Code var l = arr.length for (var i = 0, len = l; i < len; i++) { arr[i] *= 1; }