Editing @rmurphey-foo-foo-foo 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) Testing performance of different implementations for @rmurpheys challenge for creating and array of tripled string values. http://gist.github.com/576723 Who said JavaScript OCD? 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> function cubeString(str) { return str + " " + str + " " + str; } </script> <script> // 5: given the following array, create an array that contains the contents of // each array item repeated three times, with a space between each item. so, // for example, if an array item is 'foo' then the new array should contain an // array item 'foo foo foo'. (you can assume the library of your choice is // available) var myArray = ['foo', 'bar', 'baz']; </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 loopy = []; for (var i = 0, j = myArray.length; i < j; i++) { loopy.push([myArray[i], myArray[i], myArray[i]].join(' ')); } Test 2 Title Async (check if this is an asynchronous test) Code var es5 = []; function triple(item) { es5.push([item, item, item].join(' ')); } myArray.forEach(triple); Test 3 Title Async (check if this is an asynchronous test) Code function triple(item) { return [item, item, item].join(' '); } var es5 = myArray.map(triple); Test 4 Title Async (check if this is an asynchronous test) Code var results = []; $.each(myArray, function(index, item) { results.push([item, item, item].join(' ')); }); Test 5 Title Async (check if this is an asynchronous test) Code var results = $.map(myArray, function(item) { return [item, item, item].join(' '); }); Test 6 Title Async (check if this is an asynchronous test) Code var results = []; var re = /(.*)/; for (var i = 0, j = myArray.length; i < j; i++) { results.push(myArray[i].replace(re, "$1 $1 $1")); } Test 7 Title Async (check if this is an asynchronous test) Code var results = myArray.toString().replace(/(\d)/g, "$1 $1 $1").split(','); Test 8 Title Async (check if this is an asynchronous test) Code var results = myArray.toString().replace(/\d/g, function(item) { return [item, item, item].join(' '); }).split(','); Test 9 Title Async (check if this is an asynchronous test) Code var loopy = [], i = 0, item; while (item = myArray[i++]) { loopy.push(item + ' ' + item + ' ' + item); } // now that's fast Test 10 Title Async (check if this is an asynchronous test) Code var results = []; for (var i = 0, j = myArray.length; i < j; i++) { results[i] = "% % %".replace(/%/g, myArray[i]); } Test 11 Title Async (check if this is an asynchronous test) Code for (var loopy = [], i = myArray.length, str; i--; loopy[i] = (str = myArray[i]) + " " + str + " " + str); // now that's even faster ;-) Test 12 Title Async (check if this is an asynchronous test) Code var map = myArray.map(cubeString); // don't create a function for each // test execution!!! Test 13 Title Async (check if this is an asynchronous test) Code for (var loopy = [], i = myArray.length, str; str = myArray[--i]; loopy[i] = str + " " + str + " " + str); // should be slower than slim one // due non boolean/integer // expression in the middle Test 14 Title Async (check if this is an asynchronous test) Code for (var loopy = [], i = myArray.length, str, str2; i--; loopy[i] = (str2 = (str = myArray[i]) + " ") + str2 + str); Test 15 Title Async (check if this is an asynchronous test) Code for (var loopy = [], i = myArray.length, str; i--; loopy[i] = (str = myArray[i]).concat(" ", str, ' ', str));