appendChild vs. DocumentFragment vs. innerHTML

JavaScript performance comparison

Revision 34 of this test case created by David

Info

Comparing 5 different ways to dynamically insert new elements into the DOM tree.

Preparation code

<div id="test1"></div>
<script>
Benchmark.prototype.setup = function() {
    window.content = {
        // a simple string to be attached via innerHTML
        string: '<div>Testing innerHtml and DOM-based methods on <strong>massive</strong> add.</div>',
   
        // a node to be attached using DOM methods
        dom: (function () {
            var node, strong;
            node = document.createElement('div');
            node.appendChild(document.createTextNode('Testing innerHtml and DOM-based methods on '));
            strong = document.createElement('strong');
            strong.appendChild(document.createTextNode('massive'));
            node.appendChild(strong);
            node.appendChild(document.createTextNode(' add.'));
            return node;
        })(),
   
        makeDom: function () {
            var node, strong;
            node = document.createElement('div');
            node.appendChild(document.createTextNode('Testing innerHtml and DOM-based methods on '));
            strong = document.createElement('strong');
            strong.appendChild(document.createTextNode('massive'));
            node.appendChild(strong);
            node.appendChild(document.createTextNode(' add.'));
            return node;
        }
    };
};

Benchmark.prototype.teardown = function() {
    document.getElementById('test1').innerHTML = '';
};
</script>

Preparation code output

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
innerHTML
var div1 = document.getElementById("test1"),
    string = window.content.string,
    html = [];

for (var i = 0; i < 10000; i++) {
    html.push(string);
}

div1.innerHTML = html.join('');
pending…
DocumentFragment with cloned node
var div1 = document.getElementById("test1"),
    dom = window.content.dom,
    frag = document.createDocumentFragment();

for (var i = 0; i < 10000; i++) {
    frag.appendChild(dom.cloneNode(true));
}

div1.appendChild(frag);
pending…
DocumentFragment with created node
var div1 = document.getElementById("test1"),
    makeDom = window.content.makeDom
    frag = document.createDocumentFragment();

for(var i=0; i<1000; i++){
    frag.appendChild(makeDom());
}

div1.appendChild(frag);
pending…
appendChild with created node
var div1 = document.getElementById("test1"),
    makeDom = window.content.makeDom;

for(var i=0; i<1000; i++){
    div1.appendChild(makeDom());
}
 
pending…
appendChild with cloned node
var div1 = document.getElementById("test1"),
    dom = window.content.dom;

for(var i=0; i<1000; i++){
    div1.appendChild(dom.cloneNode(true));
}
 
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