innerHTML or DOM (v29)

Revision 29 of this benchmark created on


Description

Here we compare hand-optimised DOM and innerHTML element creation alongside the already fast 'Sugared DOM' library (http://blog.fastmail.fm/2012/02/20/building-the-new-ajax-mail-ui-part-2-better-than-templates-building-highly-dynamic-web-pages/).

This perf test introduces 'Sugared DOM Optimized'. This is the same lightweight but powerful HTML templating library as Sugared DOM. However, by removing superfluous querySelector-like syntax (*) from DOM element creation we obtain a HTML templating library that is regularly _faster_ than native innerHTML usage. Wow.


  • So element creation written as el('div#myId') in Sugared DOM should instead be written as el('div', { id: 'myId'}) in Sugared DOM Optimized.

Preparation HTML

<script type="text/javascript">
"use strict";

var sugaredDOM = (function () {

var doc = document;

var directProperties = {
    'class': 'className',
    className: 'className',
    defaultValue: 'defaultValue',
    'for': 'htmlFor',
    html: 'innerHTML',
    text: 'textContent',
    value: 'value'
};

var booleanProperties = {
    checked: 1,
    defaultChecked: 1,
    disabled: 1,
    multiple: 1,
    selected: 1
};

var setProperty = function ( el, key, value ) {
    var prop = directProperties[ key ];
    if ( prop ) {
        el[ prop ] = ( value == null ? '' : '' + value );
    } else if ( booleanProperties[ key ] ) {
        el[ key ] = !!value;
    } else if ( value == null ) {
        el.removeAttribute( key );
    } else {
        el.setAttribute( key, '' + value );
    }
};

var appendChildren = function ( el, children ) {
    var i, l, node;
    for ( i = 0, l = children.length; i < l; i += 1 ) {
        node = children[i];
        if ( node ) {
            if ( node instanceof Array ) {
                appendChildren( el, node );
            } else {
                if ( typeof node === 'string' ) {
                    node = doc.createTextNode( node );
                }
                el.appendChild( node );
            }
        }
    }
};

var splitter = /(#|\.)/;

var create = function ( tag, props, children ) {
    if ( props instanceof Array ) {
        children = props;
        props = null;
    }
    
    var parts, name, el,
        i, j, l, node, prop;
    
    if ( splitter.test( tag ) ) {
        parts = tag.split( splitter );
        tag = parts[0];
        if ( !props ) { props = {}; }
        for ( i = 1, j = 2, l = parts.length; j < l; i += 2, j += 2 ) {
            name = parts[j];
            if ( parts[i] === '#' ) {
                props.id = name;
            } else {
                props.className = props.className ?
                    props.className + ' ' + name : name;
            }
        }
    }
    
    el = doc.createElement( tag );
    if ( props ) {
        for ( prop in props ) {
            setProperty( el, prop, props[ prop ] );
        }
    }
    if ( children ) {
        appendChildren( el, children );
    }
    return el;
};

return create;

}() );

var sugaredDOMOptimized = ( function () {
 
var doc = document;
 
var directProperties = {
    'class': 'className',
    className: 'className',
    defaultValue: 'defaultValue',
    'for': 'htmlFor',
    html: 'innerHTML',
    text: 'textContent',
    value: 'value'
};
 
var booleanProperties = {
    checked: 1,
    defaultChecked: 1,
    disabled: 1,
    multiple: 1,
    selected: 1
};
 
var setProperty = function ( el, key, value ) {
    var prop = directProperties[ key ];
    if ( prop ) {
        el[ prop ] = ( value == null ? '' : '' + value );
    } else if ( booleanProperties[ key ] ) {
        el[ key ] = !!value;
    } else if ( value == null ) {
        el.removeAttribute( key );
    } else {
        el.setAttribute( key, '' + value );
    }
};
 
var appendChildren = function ( el, children ) {
    var i, l, node;
    for ( i = 0, l = children.length; i < l; i += 1 ) {
        node = children[i];
        if ( node ) {
            if ( node instanceof Array ) {
                appendChildren( el, node );
            } else {
                if ( typeof node === 'string' ) {
                    node = doc.createTextNode( node );
                }
                el.appendChild( node );
            }
        }
    }
};
 
var create = function ( tag, props, children ) {
    if ( props instanceof Array ) {
        children = props;
        props = null;
    }
    
    var el = doc.createElement( tag );
    if ( props ) {
        for ( var prop in props ) {
          setProperty( el, prop, props[ prop ] );
        }
    }
    if ( children ) {
        appendChildren( el, children );
    }
    return el;
};
 
return create;
 
}() );

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 genDOMWithSugar = function ( data ) {
    var el = sugaredDOM;
    return el( 'div', {
        className: 'Foo' +
            ( data.isSelected ? ' selected' : '' ) +
            ( data.isActive ? ' active' : '' )
    }, [
        el( 'label', [
            el( 'input', {
                type: 'checkbox',
                checked: data.isSelected ? 'checked': ''
            })
        ]),
        el( 'button.button', {
            title: 'A title',
            text: 'The button text'
        }),
        el( 'a', {
            href: data.href
        }, [
            el( 'span', {
                className: 'lorem' +
                    ( data.total > 1 ? ' ipsum' : '' )
            }, [
                el( 'span.dolores', {
                    unselectable: 'on'
                }, [
                    data.text
                ]),
                data.total > 1 ? el( 'span.total', {
                    unselectable: 'on',
                    text: '(' + data.total + ')'
                }) : null
            ]),
            data.yes ? el( 'span.yes', {
                text: '*'
            }) : null,
            el( 'span.text', {
                unselectable: 'on'
            }, [
                data.total > 1 ? data.text : null
            ]),
            el( 'time', {
                unselectable: 'on',
                text: data.displayDate,
                title: data.displayDate
            }),
            el( 'span.preview', {
                unselectable: 'on',
                text: data.preview
            })
        ])
    ] );
};

var genDOMWithSugarOptimized = function ( data ) {
    var el = sugaredDOMOptimized;
    return el( 'div', {
        class: 'Foo' +
            ( data.isSelected ? ' selected' : '' ) +
            ( data.isActive ? ' active' : '' )
    }, [
        el( 'label', [
            el( 'input', {
                type: 'checkbox',
                checked: data.isSelected ? 'checked': ''
            })
        ]),
        el( 'button', {
            class: 'button',
            title: 'A title',
            text: 'The button text'
        }),
        el( 'a', {
            href: data.href
        }, [
            el( 'span', {
                class: 'lorem' +
                    ( data.total > 1 ? ' ipsum' : '' )
            }, [
                el( 'span', {
                    className: 'dolores',
                    unselectable: 'on'
                }, [
                    data.text
                ]),
                data.total > 1 ? el( 'span', {
                    class: 'total',
                    unselectable: 'on',
                    text: '(' + data.total + ')'
                }) : null
            ]),
            data.yes ? el( 'span', {
                class: 'yes',
                text: '*'
            }) : null,
            el( 'span', {
                class: 'text',
                unselectable: 'on'
            }, [
                data.total > 1 ? data.text : null
            ]),
            el( 'time', {
                unselectable: 'on',
                text: data.displayDate,
                title: data.displayDate
            }),
            el( 'span', {
                class: 'preview',
                unselectable: 'on',
                text: data.preview
            })
        ])
    ] );
};

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

var genDOMWithInnerHTML = function ( data ) {
    var div = document.createElement( 'div' );
    div.innerHTML = '<div class="Foo' +
            (data.isSelected ? ' selected' : '') +
            (data.isActive ? ' active' : '') + '">' +
        '<label><input type="checkbox" checked="' +
        (data.isSelected ? 'checked': '') + '"></label>' +
        '<button class="button" title="A title">The button text</button>' +
        '<a href="' + data.href + '"><span class="lorem' +
            ( data.total > 1 ? ' ipsum' : '' ) +
            '"><span class="dolores" unselectable="on">' + data.text +
            '</span>' + ( data.total > 1 ?
                '<span class="total" unselectable="on">(' + data.total + ')</span>' :
                '') +
            '</span>' + (data.yes ? '<span class="yes">*</span>' : '') +
            '<span class="text" unselectable="on">' +
            (data.total > 1 ? data.text : null) +
            '</span>' +
            '<time unselectable="on" title="' + data.displayDate + '">' +
            data.displayDate +
            '</time><span class="preview" unselectable="on">' + data.preview +
            '</span></a></div>';
    return div.firstChild;
};

</script>

Test runner

Ready to run.

Testing in
TestOps/sec
innerHTML
genDOMWithInnerHTML( data );
ready
DOM
genDOM( data );
ready
Sugared DOM
genDOMWithSugar( data );
ready
Sugared DOM Optimized
genDOMWithSugarOptimized( data );
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.