Editing handlebars each vs concatenation vs array join 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) 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="//cloud.github.com/downloads/wycats/handlebars.js/handlebars-1.0.rc.1.js"> </script> <script src="//underscorejs.org/underscore-min.js"> </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 optionTemplateString = '{{#each options}}<option value="{{value}}">{{label}}</option>{{/each}}'; var optionTemplate = Handlebars.compile(optionTemplateString); target = { options: [] }; resultRegex = /^<option.*<\/option>$/; var pad = function(number, length) { var str = '' + number; while (str.length < length) { str = '0' + str; } } var suffix = ''; for (var i = 0; i < 1000; i++) { suffix = pad(i, 5); target.options.push({ value: 'foo' + suffix, label: 'bar' + suffix }); } Define teardown for all tests (runs after each clocked test loop, outside of the timed code region) (see FAQ) if (!resultRegex.test(result)) { throw new Error('Unexpected output'); } Code snippets to compare Test 1 Title Async (check if this is an asynchronous test) Code result = Handlebars.compile('{{#each options}}' + Handlebars.compile(optionTemplateString) + '{{/each}}')(target); Test 2 Title Async (check if this is an asynchronous test) Code var options = target.options; var length = options.length; result = ''; for (var i = 0; i < length; i++) { result += optionTemplate(options[i]); } Test 3 Title Async (check if this is an asynchronous test) Code var options = target.options; var length = options.length; result = []; for (var i = 0; i < length; i++) { result.push(optionTemplate(options[i])); } result = result.join(''); Test 4 Title Async (check if this is an asynchronous test) Code result = _.map(target.options, optionTemplate).join('');