Editing first-pass 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="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"> </script> <script src="//cdnjs.cloudflare.com/ajax/libs/underscore.js/1.4.2/underscore-min.js"></script> <table id="test_table"> <tbody id="test_tbody"> Test </tbody> </table> <style> .active{ background:red; font-size:30px; } </style> <script type="text/template" id="template"> <% for(var i = 0; i < rows; i++){ %> <tr> <% for(var j = 0; j < data.length; j++){ %> <td><%= data[j] %></td> <% } %> </tr> <% } %> </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) // Tables var numItems = 200; // items/cols var rows = 200; var tbody = document.getElementById('test_tbody'); var $tbody = $('#test_tbody'); var tr, td; var $tr, $td; // Data arrays var dataArray = []; var dataArrayObject = []; // Fragments var frag = document.createDocumentFragment(); var frag2 = document.createDocumentFragment(); // Populate data for (var i = 0; i < numItems; i++) { dataArray[i] = i; } for (var i = 0; i < numItems; i++) { dataArrayObject[i] = {}; dataArrayObject[i]["id"] = i; } var predefinedLengthArray = new Array(50); for (i = 0; i < numItems; i++) { predefinedLengthArray[i] = dataArray[i]; } function drawDocumentFragTable(cols) { for (var i = 0; i < rows; i++) { tr = document.createElement('tr'); for (var j = 0; j < cols.length; j++) { td = document.createElement('td'); td.appendChild(document.createTextNode(cols[j])); frag2.appendChild(td); } tr.appendChild(frag2); frag.appendChild(tr); } tbody.appendChild(frag); } function eventsNoDelegation() { $('table td').on('click', function () { $(this).toggleClass('active'); }); } function eventsWithDelegation() { $('table').on('click', 'td', function () { $(this).toggleClass('active'); }); } // Array object, no delegation, jQuery for table creation. var moduleA = function () { return { data: dataArrayObject, init: function () { this.addTable(); this.addEvents(); }, addTable: function () { for (var i = 0; i < rows; i++) { $tr = $('<tr></tr>'); for (var j = 0; j < this.data.length; j++) { $tr.append('<td>' + this.data[j]["id"] + '</td>'); } $tr.appendTo($tbody); } }, addEvents: function () { eventsNoDelegation(); } }; }(); // Simple array, no delegation, jQuery for table creation var moduleB = function () { return { data: dataArray, init: function () { this.addTable(); this.addEvents(); }, addTable: function () { for (var i = 0; i < rows; i++) { $tr = $('<tr></tr>'); for (var j = 0; j < this.data.length; j++) { $tr.append('<td>' + this.data[j]["id"] + '</td>'); } $tr.appendTo($tbody); } }, addEvents: function () { eventsNoDelegation(); } }; }(); // Simple array, delegation, jQuery for table creation. var moduleC = function () { return { data: dataArray, init: function () { this.addTable(); this.addEvents(); }, addTable: function () { for (var i = 0; i < rows; i++) { $tr = $(document.createElement('tr')); for (var j = 0; j < this.data.length; j++) { $tr.append($(document.createElement('td')).text(this.data[j]["id"])); } $tr.appendTo($tbody); } }, addEvents: function () { eventsWithDelegation(); } }; }(); // Simple array, delegation, documentFragment var moduleD = function () { return { data: dataArray, init: function () { this.addTable(); this.addEvents(); }, addTable: function () { drawDocumentFragTable(this.data); }, addEvents: function () { eventsWithDelegation(); } }; }(); // Simple array, delegation, documentFragment, prototypes moduleE = function () {}; moduleE.prototype.data = dataArray; moduleE.prototype.init = function () { this.addTable(); this.addEvents(); }; moduleE.prototype.addTable = function () { drawDocumentFragTable(this.data); }; moduleE.prototype.addEvents = function () { eventsWithDelegation(); }; var modE = new moduleE(); // Same, but with a predefined length array moduleF = function () {}; moduleF.prototype.data = predefinedLengthArray; moduleF.prototype.init = function () { this.addTable(); this.addEvents(); }; moduleF.prototype.addTable = function () { drawDocumentFragTable(this.data); }; moduleF.prototype.addEvents = function () { eventsWithDelegation(); }; var modF = new moduleF(); // Same, but with underscore templating moduleG = function () {}; moduleG.prototype.data = dataArray; moduleG.prototype.init = function () { this.addTable(); this.addEvents(); }; moduleG.prototype.addTable = function () { var template = _.template($('#template').text()); var html = template({'data' : this.data, 'rows': rows}); $tbody.append(html); }; moduleG.prototype.addEvents = function () { eventsWithDelegation(); }; var modG = new moduleG(); Define teardown for all tests (runs after each clocked test loop, outside of the timed code region) (see FAQ) rows = null; dataArray = null; dataArrayObject = null; predefinedLengthArray = null; $tbody.empty(); tbody = null; $tbody = null; tr = null; td = null; $tr = null; $td = null; frag = null; frag2 = null; moduleA = null; moduleB = null; moduleC = null; moduleD = null; moduleE = null; moduleF = null; moduleG = null; modE = null; modF = null; modG = null; // Remove all event handlers for these $("table td").off("click", "**"); Code snippets to compare Test 1 Title Async (check if this is an asynchronous test) Code // Module A - no event delegation, array objects, jQuery for table creation moduleA.init(); Test 2 Title Async (check if this is an asynchronous test) Code // Module B - no event delegation, simple arrays, jQuery for table creation moduleB.init(); Test 3 Title Async (check if this is an asynchronous test) Code // Module C - event delegation, simple arrays, jQuery for table creation moduleC.init(); Test 4 Title Async (check if this is an asynchronous test) Code // Module D - event delegation, simple arrays, documentFragment for table creation moduleD.init();