appendChild vs. DocumentFragment vs. innerHTML (v52)

Revision 52 of this benchmark created on


Description

Comparing three different ways to dynamically insert content into the DOM.

Preparation HTML

<div id="test"></div>
<script>
  function text(node, txt) {
   node.appendChild(document.createTextNode(txt));
   return node;
  }
  
  var o = document.getElementById('test'),
      aElms = [];
var elm = document.createElement('a');
elm.href='http://google.com';
elm.textContent = 'hello';
  for(var i=0;i<100;i++){
aElms.push(elm.cloneNode(true));
}
</script>

Teardown


    o.innerHTML = '';
  

Test runner

Ready to run.

Testing in
TestOps/sec
appendChild
for (var i = 0, imax = aElms.length; i < imax; i++) {
  o.appendChild(aElms[i]);
}
ready
DocumentFragment
oFrag = document.createDocumentFragment();
for (var i = 0, imax = aElms.length; i < imax; i++) {
  oFrag.appendChild(aElms[i]);
}
o.appendChild(oFrag);
ready
innerHTML
var html = '';
for (var i = 0, imax = aElms.length; i < imax; i++) {
  html += aElms[i].outerHTML;
}
o.innerHTML = html;
ready

Revisions

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