innerHTML vs createElement test

JavaScript performance comparison

Revision 49 of this test case created by

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<div id="container">
</div>
      
<script>
Benchmark.prototype.setup = function() {
  document.getElementById('container').innerHTML = '';

};

Benchmark.prototype.teardown = function() {
  document.getElementById('container').innerHTML = '';

};
</script>

Preparation code output

<div id="container"> </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
100 element innerHTML
var container = document.getElementById('container');


for (var i = 0; i < 100; i++) {
  var div = document.createElement('div');

  div.innerHTML = '<div><span>' + i + '</span></div>';
  container.appendChild(div);
}


pending…
100 element deep cloneNode bulk add
var container = document.getElementById('container');
var outerdiv = document.createDocumentFragment();
var protodiv = document.createDocumentFragment();
var protospan2 = document.createElement('div');
var protospan3 = document.createElement('div');
var protospan = document.createElement('span');
protospan2.appendChild(protospan);
protospan3.appendChild(protospan2);
protodiv.appendChild(protospan3);


for (var i = 0; i < 100; i++) {
  var div = protodiv.cloneNode(true);

  div.childNodes[0].childNodes[0].childNodes[0].innerHTML = i;
  outerdiv.appendChild(div);

}

container.appendChild(div);
pending…
100 element create fragment
var container = document.getElementById('container');
var outerdiv = document.createDocumentFragment();



for (var i = 0; i < 100; i++) {
  var protodiv = document.createDocumentFragment();
var protospan2 = document.createElement('div');
var protospan3 = document.createElement('div');
var protospan = document.createElement('span');
protospan2.appendChild(protospan);
protospan3.appendChild(protospan2);
protodiv.appendChild(protospan3);
  protospan.innerHTML = i;
  container.appendChild(protodiv);

}

pending…
100 element create
var container = document.getElementById('container');




for (var i = 0; i < 100; i++) {
var protospan2 = document.createElement('div');
var protospan3 = document.createElement('div');
var protospan = document.createElement('span');
protospan2.appendChild(protospan);
protospan3.appendChild(protospan2);
  protospan.innerHTML = i;
  container.appendChild(protospan3);

}
pending…
100 element create fragment append outsider the loop
var container = document.getElementById('container');
var outerdiv = document.createDocumentFragment();



for (var i = 0; i < 100; i++) {

var protospan2 = document.createElement('div');
var protospan3 = document.createElement('div');
var protospan = document.createElement('span');
protospan.innerHTML = i;
protospan2.appendChild(protospan);
protospan3.appendChild(protospan2);
outerdiv.appendChild(protospan3);

}
  container.appendChild(outerdiv);
pending…
100 element create inner fragment append outsider the loop
var container = document.getElementById('container');
var outerdiv = document.createDocumentFragment();



for (var i = 0; i < 100; i++) {
  var protodiv = document.createDocumentFragment();
var protospan2 = document.createElement('div');
var protospan3 = document.createElement('div');
var protospan = document.createElement('span');
protospan2.appendChild(protospan);
protospan3.appendChild(protospan2);
protodiv.appendChild(protospan3);
  protospan.innerHTML = i;

outerdiv.appendChild(protodiv);

}
  container.appendChild(outerdiv);
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