appendChild vs. DocumentFragment vs. innerHTML

JavaScript performance comparison

Revision 11 of this test case created by chris bodar

Info

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

Preparation code

<div id="test1"></div>
<div id="test2"></div>
<div id="test3"></div>
<div id="test4"></div>
<script>
  // You can read this: <http://ejohn.org/blog/dom-documentfragments/>
 
  function text(node, txt) {
    node.appendChild(document.createTextNode(txt));
    node.setAttribute('class', 'hello-world');
    return node;
  }
 
  function cleanUp(node) {
    node.innerHTML = "";
  }
 
  var content = [
    document.createElement("hr"),
    text(document.createElement("b"), "Links:"),
    document.createTextNode(" "),
    text(document.createElement("a"), "Link A"),
    document.createTextNode(" | "),
    text(document.createElement("a"), "Link B"),
    document.createTextNode(" | "),
    text(document.createElement("a"), "Link C")
  ];
  var wrapperDiv = document.createElement("div")
  for (var i = 0, j = content.length; i < j; i++) {
    wrapperDiv.appendChild(content[i].cloneNode(true));
  }
  var wrapperFragment = document.createDocumentFragment()
  for (var i = 0, j = content.length; i < j; i++) {
    wrapperFragment.appendChild(content[i].cloneNode(true));
  }
  var htmlString = '<hr><b class="hello-world">Links:</b> <a class="hello-world">Link A</a> | <a class="hello-world">Link B</a> | <a class="hello-world">Link C</a>'
 
  //Note += innerHTML destroys the pre-existing html and rebuilds it so it is not a fair/safe comparison
</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
appendChild
var div1 = document.getElementById("test1");

for (var i = 0, j = content.length; i < j; i++) {
  div1.appendChild(content[i].cloneNode(true));
}

cleanUp(div1);
pending…
DocumentFragment
var div2 = document.getElementById("test2");

div2.appendChild(wrapperFragment.cloneNode(true));

cleanUp(div2);
pending…
innerHTML
var div3 = document.getElementById("test3");

div3.innerHTML = htmlString ;

cleanUp(div3);
pending…
“Offline” node method
var div4 = document.getElementById("test4"),
    container = document.createElement("div");
//This does NOT (and has never) created the same markup so is a null test (it creates an extra div)
for (var i = 0, j = content.length; i < j; i++) {
  container.appendChild(content[i].cloneNode(true));
}

div4.appendChild(container);

cleanUp(div4);
pending…
insertAdjacentHTML (not all browsers)
var div3 = document.getElementById("test3");
  div3.insertAdjacentHTML("AfterBegin",htmlString);


cleanUp(div3);
pending…
Offline node (removed from tree then reappended)
var div4 = document.getElementById("test4"),
    replacementNode = div4.cloneNode(true),
parentNode = div4.parentNode
parentNode.replaceChild(replacementNode,div4)
for (var i = 0, j = content.length; i < j; i++) {
  div4.appendChild(content[i].cloneNode(true));
}

parentNode.replaceChild(div4,replacementNode);
cleanUp(div4)
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