Editing innerHTML or DOM 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) Most dynamic websites use innerHTML because they think it's faster. But is it? This compares hand-optimised DOM and innerHTML along with a sugared DOM and Handlebars templated version. 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 type="text/javascript"> "use strict"; var data = { isSelected: true, isActive: false, href: 'http://www.google.com', total: 4, displayDate: '28th December 2011', text: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.', preview: 'Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.' }; var genDOM = function ( data ) { var doc = document, div = doc.createElement( 'div' ); div.className = 'Foo' + ( data.isSelected ? ' selected' : '' ) + ( data.isActive ? ' active' : '' ); var label = doc.createElement( 'label' ), input = doc.createElement( 'input' ); input.setAttribute( 'type', 'checkbox' ); input.checked = !!data.isSelected; label.appendChild( input ); div.appendChild( label ); var button = doc.createElement( 'button' ); button.className = 'button'; button.setAttribute( 'title', 'A title' ); button.textContent = 'The button text'; div.appendChild( button ); var a = doc.createElement( 'a' ); a.setAttribute( 'href', data.href ); var span = doc.createElement( 'span' ); span.className = 'lorem' + ( data.total > 1 ? ' ipsum' : '' ); var span2 = doc.createElement( 'span' ); span2.className = 'dolores'; span2.setAttribute( 'unselectable', 'on' ); span2.appendChild( doc.createTextNode( data.text ) ); span.appendChild( span2 ); if ( data.total > 1 ) { span2 = doc.createElement( 'span' ); span2.className = 'total'; span2.setAttribute( 'unselectable', 'on' ); span2.textContent = '(' + data.total + ')'; span.appendChild( span2 ); } a.appendChild( span ); if ( data.yes ) { span = doc.createElement( 'span' ); span.className = 'yes'; span.textContent = '*'; a.appendChild( span ); } span = doc.createElement( 'span' ); span.className = 'text'; span.setAttribute( 'unselectable', 'on' ); if ( data.total > 1 ) { span.appendChild( doc.createTextNode( data.text ) ); } a.appendChild( span ); var time = doc.createElement( 'time' ); time.setAttribute( 'unselectable', 'on' ); time.textContent = data.displayDate; time.setAttribute( 'title', data.displayDate ); a.appendChild( time ); span = doc.createElement( 'span' ); span.className = 'preview'; span.setAttribute( 'unselectable', 'on' ); span.textContent = data.preview; a.appendChild( span ); div.appendChild( a ); return div; }; </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 genDOM( data );