innerHTML vs createElement (Lots of nesting)

JavaScript performance comparison

Revision 12 of this test case created

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 innerHTMLstr = '<div>';
for (var i = 0; i < 100; i++) {
  innerHTMLstr += '<header><section><div><span><a href="http://www.google.com">' + i + '</a></span></div></section></header>';
}
innerHTMLstr+='</div>';

document.getElementById('container').innerHTML = innerHTMLstr;
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 header = document.createElement('header');
  var section = document.createElement('section');
  var div = document.createElement('div');
  var span = document.createElement('span');
  var link = document.createElement('a');

  link.innerHTML = i;
  span.appendChild(link);
  div.appendChild(span);
  section.appendChild(div);
  header.appendChild(section);
  outerdiv.appendChild(header);

}
pending…
100 element createElement bulk add
var container = document.getElementById('container');
var outerdiv = document.createElement('div');

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

  link.innerHTML = i;
  span.appendChild(link);
  div.appendChild(span);
  section.appendChild(div);
  header.appendChild(section);
  outerdiv.appendChild(header);

}

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. Here’s a list of current revisions for this page:

0 comments

Add a comment