cloneNode vs createElement vs innerHTML

JavaScript performance comparison

Revision 32 of this test case created by Larry Davis

Preparation code

<div id="target"></div>

<script>
  function generateNodes() {
    var el0 = document.createElement("section");
    el0.setAttribute("class", "section");
    var el2 = document.createElement("header");
    el2.setAttribute("class", "header");
    el2.textContent = "Header";
    el0.appendChild(el2);
    var el4 = document.createElement("article");
    el4.setAttribute("class", "article");
    var el6 = document.createElement("p");
    el6.textContent = "Paragraph 1";
    el4.appendChild(el6);
    var el8 = document.createElement("p");
    el8.textContent = "Paragraph 2";
    el4.appendChild(el8);
    var el10 = document.createElement("p");
    el10.textContent = "Paragraph 3";
    el4.appendChild(el10);
    var el12 = document.createElement("p");
    el12.textContent = "Paragraph 4";
    el4.appendChild(el12);
    var el14 = document.createElement("p");
    el14.textContent = "Paragraph 5";
    el4.appendChild(el14);
    var el16 = document.createElement("ul");
    el16.setAttribute("class", "list");
    var el18 = document.createElement("li");
    el18.textContent = "Item 1";
    el16.appendChild(el18);
    var el20 = document.createElement("li");
    el20.textContent = "Item 2";
    el16.appendChild(el20);
    var el22 = document.createElement("li");
    el22.textContent = "Item 3";
    el16.appendChild(el22);
    var el24 = document.createElement("li");
    el24.textContent = "Item 4";
    el16.appendChild(el24);
    var el26 = document.createElement("li");
    el26.textContent = "Item 5";
    el16.appendChild(el26);
    el4.appendChild(el16);
    el0.appendChild(el4);
    var el30 = document.createElement("footer");
    el30.setAttribute("class", "footer");
    el30.textContent = "Footer";
    el0.appendChild(el30);
    return el0;
  }

  var div = generateNodes();

  var html = '<header class="header">Header</header><article class="article"><p>Paragraph 1</p><p>Paragraph 2</p><p>Paragraph 3</p><p>Paragraph 4</p><p>Paragraph 5</p><ul class="list"><li>Item 1</li><li>Item 2</li><li>Item 3</li><li>Item 4</li><li>Item 5</li></ul></article><footer class="footer">Footer</footer>';

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

};
</script>

Preparation code output

<div id="target"></div>

Test runner

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

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
cloneNode()
var newDiv = div.cloneNode(true);
target.appendChild(newDiv);
pending…
createElement()
var newDiv = generateNodes();
target.appendChild(newDiv);
pending…
innerHTML
target.innerHTML = html;
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.

0 Comments