cloneNode vs createElement Performance (v15)

Revision 15 of this benchmark created on


Description

check speeds with different elements

Preparation HTML

<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>

Teardown


    document.body.removeChild(document.getElementById('clone'));
  

Test runner

Ready to run.

Testing in
TestOps/sec
Clone Deep
CloneAndAppend();
ready
Create
CreateAndAppend();
ready

Revisions

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