Editing Performance comparison of jQuery Template vs underscore vs string append for table 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) To test performance of jQuery template for outputting 100 rows each with 10 columns where the columns have fields that can trigger events. 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.6.2/jquery.min.js"> </script> <script src="http://github.com/jquery/jquery-tmpl/raw/master/jquery.tmpl.js"> </script> <script src="http://documentcloud.github.com/underscore/underscore.js"> </script> <script src="http://github.com/mhevery/mustache.js/raw/dot-notation/mustache.js"> </script> <script src="http://github.com/pvande/Milk/raw/master/dist/v1.2.0/milk.js"> </script> <script src="https://raw.github.com/Shopify/batman/master/lib/dist/batman.min.js"></script> <script type="text/javascript"> $(function() { $('body').delegate('#table .edit a, #table .select a', 'click', function() { var $td = $(this).closest('td'); $td.find('input, select').show(); $td.find('a').hide(); }); $('body').delegate('#table .edit .cancel, $table .select .cancel', 'click', function() { var $td = $(this).closest('td'); $td.find('input, select').hide(); $td.find('a').show(); }); }); </script> <script type="text/x-jquery-tmpl" id="tmplRow"> < tr > < td > < input type = "checkbox" value = "${id}" / > < /td> <td>${firstName} ${lastName}</td > < td class = "edit" > < a > Edit < /a> <input style="display:none;" type="hidden" value="Blah" / > < input class = "cancel" type = "button" value = "cancel" / > < /td> <td class="select"> <a>Select</a > < select style = "display:none;" > < option > 0 < /option> <option>1</option > < option > 2 < /option> <option>3</option > < option > 4 < /option> <option>5</option > < option > 6 < /option> <option>7</option > < option > 8 < /option> <option>9</option > < option > 10 < /option> </select > < input class = "cancel" type = "button" value = "cancel" / > < /td> <td>More string</td > < td > More string < /td> <td>More string</td > < td > More string < /td> <td>More string</td > < td > More string < /td> </tr > </script> <table id="batman_template"> <tr data-foreach-person="data"> <td><input type="checkbox" data-bind="person.id"/></td> <td data-bind="person.firstName | append ' ' | append person.lastName"></td > <td class="edit"> <a> Edit </a> <input style="display:none;" type="hidden" value="Blah"/> <input class="cancel" type="button" value="cancel" /> </td> <td class="select"> <a>Select</a> <select style="display:none;"> <option>0</option> <option>1</option > < option > 2 </option> <option>3</option > < option > 4 </option> <option>5</option > < option > 6 </option> <option>7</option > < option > 8 </option> <option>9</option > < option > 10 </option> </select> <input class="cancel" type="button" value="cancel" /> </td> <td>More string</td > <td> More string </td> <td>More string</td > <td> More string </td> <td>More string</td > <td> More string </td> </tr> </table> <script> var data = []; for (var i = 0; i < 100; i++) { var row = { id: i, firstName: 'john', lastName: 'doe' }; data.push(row); } $.template("savedTmpl", document.getElementById("tmplRow").innerHTML); var underscoreTmpl = "\ <%for(var i=0;i<rows.length;i++){%>\ <%var row=rows[i];%> \ <tr> \ <td> <input type = 'checkbox' value = '<%= row.id%>' /> </td>\ <td><%= row.firstname%> <%= row.lastname%></td> \ <td class = 'edit'> \ <a> Edit </a>\ <input style='display:none;' type='hidden' value='Blah' /> \ <input class = 'cancel' type = 'button' value = 'cancel' /> \ </td>\ <td class='select'>\ <a>Select</a> \ <select style = 'display:none;'> \ <option>0</option>\ <option>1</option> \ <option>2</option>\ <option>3</option> \ <option>4</option>\ <option>5</option> \ <option>6</option>\ <option>7</option> \ <option>8</option>\ <option>9</option> \ <option>10</option>\ </select> \ <input class = 'cancel' type = 'button' value = 'cancel' /> \ </td>\ <td>More string</td> \ <td>More string</td>\ <td>More string</td> \ <td>More string</td>\ <td>More string</td> \ <td>More string</td>\ </tr ><%}%>"; var underscorePrecompiled = _.template(underscoreTmpl); var mustacheTmpl="\ {{#rows}}\ <tr> \ <td> <input type = 'checkbox' value = '{{id}}' /> </td>\ <td>{{firstname}} {{lastname}}</td> \ <td class = 'edit'> \ <a> Edit </a>\ <input style='display:none;' type='hidden' value='Blah' /> \ <input class = 'cancel' type = 'button' value = 'cancel' /> \ </td>\ <td class='select'>\ <a>Select</a> \ <select style = 'display:none;'> \ <option>0</option>\ <option>1</option> \ <option>2</option>\ <option>3</option> \ <option>4</option>\ <option>5</option> \ <option>6</option>\ <option>7</option> \ <option>8</option>\ <option>9</option> \ <option>10</option>\ </select> \ <input class = 'cancel' type = 'button' value = 'cancel' /> \ </td>\ <td>More string</td> \ <td>More string</td>\ <td>More string</td> \ <td>More string</td>\ <td>More string</td> \ <td>More string</td>\ </tr >{{/rows}}"; var milkTmpl="\ {{#rows}}\ <tr> \ <td> <input type = 'checkbox' value = '{{id}}' /> </td>\ <td>{{firstname}} {{lastname}}</td> \ <td class = 'edit'> \ <a> Edit </a>\ <input style='display:none;' type='hidden' value='Blah' /> \ <input class = 'cancel' type = 'button' value = 'cancel' /> \ </td>\ <td class='select'>\ <a>Select</a> \ <select style = 'display:none;'> \ <option>0</option>\ <option>1</option> \ <option>2</option>\ <option>3</option> \ <option>4</option>\ <option>5</option> \ <option>6</option>\ <option>7</option> \ <option>8</option>\ <option>9</option> \ <option>10</option>\ </select> \ <input class = 'cancel' type = 'button' value = 'cancel' /> \ </td>\ <td>More string</td> \ <td>More string</td>\ <td>More string</td> \ <td>More string</td>\ <td>More string</td> \ <td>More string</td>\ </tr >{{/rows}}" </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 html1= $("#tmplRow").tmpl(data) Test 2 Title Async (check if this is an asynchronous test) Code var s = []; for (var i = 0; i < 100; i++) { s.push('<tr>'); s.push('<td><input type="checkbox" value="'); s.push(data[i].id); s.push('" /></td><td>'); s.push(data[i].firstName + ' '); s.push(data[i].lastName); s.push('</td>'); s.push('<td class="edit"> <a>Edit</a> <input style="display:none;" type="hidden" value="Blah" /> <input class="cancel" type="button" value="cancel" /> </td> <td class="select"> <a>Select</a> <select style="display:none;"> <option>0</option> <option>1</option> <option>2</option> <option>3</option> <option>4</option> <option>5</option> <option>6</option> <option>7</option> <option>8</option> <option>9</option> <option>10</option> </select> <input class="cancel" type="button" value="cancel" /> </td> <td>More string</td> <td>More string</td> <td>More string</td> <td>More string</td> <td>More string</td> <td>More string</td>'); s.push('</tr>'); } var html2= s.join(''); Test 3 Title Async (check if this is an asynchronous test) Code var html3 = $.tmpl("savedTmpl", data); Test 4 Title Async (check if this is an asynchronous test) Code var html4 = underscorePrecompiled({rows: data}); Test 5 Title Async (check if this is an asynchronous test) Code Mustache.to_html(mustacheTmpl, {rows: data}); Test 6 Title Async (check if this is an asynchronous test) Code Milk.render(milkTmpl, {rows: data});