cloneNode vs createElement Performance (v30)

Revision 30 of this benchmark created by Larry Davis on


Description

Benchmark the creation of a complex DOM structure:

  1. Using createElement, setAttribute, and appendChild to create the structure
  2. Via a deep cloneNode of an existing node that represents the structure

Preparation HTML

<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();
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
Clone structure
var newDiv = div.cloneNode(true);
ready
Create structure
var newDiv = generateNodes();
ready

Revisions

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