innerHTML or DOM

JavaScript performance comparison

Revision 14 of this test case created

Info

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.

Preparation code

<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>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
DOM
genDOM( data );
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL. Here’s a list of current revisions for this page:

0 comments

Add a comment