Editing for vs forEach 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) Is it faster to use the native forEach or just loop with for? Inspired by Adrian Sutton's tests at: http://www.symphonious.net/2010/10/09/javascript-performance-for-vs-foreach/ This one adds random floating point numbers to see if the loop overhead is significant at all in the face of standard work. 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) 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, value, length, values = [], sum = 0, context = values; for (i = 0; i < 10000; i++) { values[i] = Math.random(); } function add(val) { sum += val; } function addEach(k, val) { sum += val; } var toString = Object.prototype.toString; function isArray(obj) { return toString.call(obj) === '[object Array]'; } function isObject(obj) { return toString.call(obj) === '[object Object]'; } function isString(obj) { return toString.call(obj) === '[object String]'; } function each(obj, iterator) { var key, length; if (!obj) { return; } length = obj.length; if (isArray(obj) || isString(obj)) { for(key = 0; key < length; key += 1) { iterator(obj[key], key, obj); } return obj; } if (isObject(obj)) { for(key in obj) { if (obj.hasOwnProperty(key)) { iterator(obj[key], key, obj); } } return obj; } return obj; }; function each1 (data, iterator) { var i, length = data.length; for (i = 0; i < length; i += 1) { iterator(data[i], i, data); } } function each2 (data, iterator) { var i, length = data.length; for (i = 0; i < length; ++i) { iterator(data[i], i, data); } } Define teardown for all tests (runs after each clocked test loop, outside of the timed code region) (see FAQ) i = 0; value = 0; length = 0; values = []; sum = 0; Code snippets to compare Test 1 Title Async (check if this is an asynchronous test) Code var arr = values, length = arr.length; while (length-- !== 0) { add(arr[length], length, arr); } Test 2 Title Async (check if this is an asynchronous test) Code var arr = values, length = arr.length + 1; while (--length !== 0) { add(arr[length-1], length-1, arr); } Test 3 Title Async (check if this is an asynchronous test) Code var arr = values, length = arr.length; for (var i = 0; i < length; ++i) { add(arr[i], i, arr); } Test 4 Title Async (check if this is an asynchronous test) Code each(values, add); Test 5 Title Async (check if this is an asynchronous test) Code var arr = values, length = arr.length; while (length--) { add(arr[length], length, arr); } Test 6 Title Async (check if this is an asynchronous test) Code each1(values, add); Test 7 Title Async (check if this is an asynchronous test) Code each2(values, add);