cloneNode vs createElement Performance

JavaScript performance comparison

Revision 15 of this test case created by

Preparation code

<style>
#template, #clone { display:none; };

</style>
<div id="template" class="class1 class2 class3">This is a div with an id, some classes, and text content.  Suppose it needed to be cloned.  Would it be faster to deep clone or create a new element and then set its textContent</div>

<script>
var div = document.getElementById('template'),
    text = div.textContent,
    classes = div.className;

function CloneAndAppend(){
  var el = div.cloneNode(true);
  el.setAttribute('id','clone');
  document.body.appendChild(el);
}

function CreateAndAppend(){
  var el = document.createElement('div');
  el.setAttribute('id','clone');
  el.textContent = text;
  el.className = classes;
  document.body.appendChild(el);
}
</script>
      
<script>
Benchmark.prototype.teardown = function() {
  document.body.removeChild(document.getElementById('clone'));

};
</script>

Preparation code output

<style> #template, #clone { display:none; }; </style> <div id="template" class="class1 class2 class3">This is a div with an id, some classes, and text content. Suppose it needed to be cloned. Would it be faster to deep clone or create a new element and then set its textContent</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
Clone Deep
CloneAndAppend();
pending…
Create
CreateAndAppend();
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

This is a div with an id, some classes, and text content. Suppose it needed to be cloned. Would it be faster to deep clone or create a new element and then set its textContent