innerHTML vs createElement test

JavaScript performance comparison

Revision 85 of this test case created by Mateusz Malicki

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 = '';
  window.round = 0;

};

Benchmark.prototype.teardown = function() {
  document.getElementById('container').innerHTML = '';
  if (++window.round == 10) round = 0;

};
</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 createElement bulk add
var container = document.getElementById('container');
var outerdiv = document.createElement('div');

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

  span.textContent = window.round + ':' + i;
  div.appendChild(span);
  outerdiv.appendChild(div);

}

container.appendChild(outerdiv);
pending…
100 elements in DocumentFragment
var container = document.getElementById('container');
var frag = document.createDocumentFragment();

for (var i = 0; i < 100; i++) {
  var div = document.createElement('div');
  var span = document.createElement('span');
  frag.appendChild(div);
  div.appendChild(span);
  span.appendChild(document.createTextNode(window.round + ':' + i));
}

container.appendChild(frag);
pending…
100 element createElement added in sequence
var container = document.getElementById('container');
var outerdiv = document.createElement('div');
container.appendChild(outerdiv);

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

  span.textContent = window.round + ':' + i;
  div.appendChild(span);
  outerdiv.appendChild(div);

}
pending…
100 element innerHTML
var innerHTMLstr = '<div>';
for (var i = 0; i < 100; i++) {
  innerHTMLstr += '<div><span>' + window.round + ':' + i + '</span></div>';
}
innerHTMLstr += '</div>';

document.getElementById('container').innerHTML = innerHTMLstr;
pending…
100 elements created with innerHTML and appended to parent
var parent = document.createElement('div');
for (var i = 0; i < 100; i++) {
  var el = document.createElement('div');
  el.innerHTML = '<div><span>' + window.round + ':' + i + '</span></div>';
  parent.appendChild(el);
}

document.getElementById('container').appendChild(parent);
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