innerHTML vs. cloneNode (v2)

Revision 2 of this benchmark created by aavezel on


Description

Tests various DOM construction patterns.

Preparation HTML

<style>
 .destination {display: none;}
</style>
<div id="destination1" class="destination"></div>
<div id="destination2" class="destination"></div>
<div id="destination3" class="destination"></div>
<div id="destination4" class="destination"></div>
<div id="destination5" class="destination"></div>
<script>
  var content = '<div class="test"><span style="color: red">Wooo Text!</span></div><div>More text!</div>',
      dest1 = document.getElementById("destination1"),
      dest2 = document.getElementById("destination2"),
      dest3 = document.getElementById("destination3"),
      dest4 = document.getElementById("destination4"),
      dest5 = document.getElementById("destination5");
  
  // For Element cloneNode test
  var womb = document.createElement("div");
  womb.innerHTML = content;
  
  // For doc fragment test
  var frag = document.createDocumentFragment(),
      clone = womb.cloneNode(true),
      len = clone.childNodes.length;
  while (len--) {
   frag.appendChild(clone.childNodes[0]);
  }
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
innerHTML
dest1.innerHTML = content;
ready
cloneNode
var clone = womb.cloneNode(true),
    len = clone.childNodes.length;
while (len--) {
 dest2.appendChild(clone.childNodes[0]);
}
ready
DocumentFragment
dest3.appendChild(frag.cloneNode(true));
ready
DOM post insert
var child = document.createElement('div');
child.className = "test";

var subChild = document.createElement('span');
subChild.style.color = "red";
subChild.appendChild(document.createTextNode("Wooo Text!"));
child.appendChild(subChild);
dest4.appendChild(child);


child = document.createElement('div');
child.appendChild(document.createTextNode("More text!"));
dest4.appendChild(child);
ready
DOM pre insert
var child = document.createElement('div');
dest5.appendChild(child);
child.className = "test";

var subChild = document.createElement('span');
child.appendChild(subChild);
subChild.style.color = "red";
subChild.appendChild(document.createTextNode("Wooo Text!"));

child = document.createElement('div');
dest5.appendChild(child);
child.appendChild(document.createTextNode("More text!"));
ready

Revisions

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