innerHTML vs createElement test

JavaScript performance comparison

Revision 50 of this test case created by Oria

Preparation code

<script src="//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

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
100 element innerHTML
var container = document.getElementById('container');


for (var i = 0; i < 10; 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 < 10; 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 < 10; 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 < 10; 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 < 10; 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 < 10; 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…
jquery per element
var container = document.getElementById('container');

for (var i = 0; i < 10; i++) {
  var protospan2 = $('<div class="asdf">');
  var protospan3 = $('<div>');
  var protospan = $('<span>');
  protospan2.append(protospan);
  protospan3.append(protospan2);
  protospan.html(i);
  protospan3.appendTo(container);
}
pending…
jquery eval
var container = document.getElementById('container');
var s = ''
for (var i = 0; i < 10; i++) {

  s + = '<div><span>' + i + '</span></div>';
}

$(container).append(s);
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. Here’s a list of current revisions for this page:

0 comments

Add a comment