innerHTML vs createElement test

JavaScript performance comparison

Revision 19 of this test case created

Preparation code

 
<script>
Benchmark.prototype.setup = function() {
    var container = document.createElement('div');
    document.body.appendChild(container);
   
};

Benchmark.prototype.teardown = function() {
    document.body.removeChild(container);
   
};
</script>

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 += '<div><span>' + i + '</span></div>';
}
innerHTMLstr+='</div>';

container.innerHTML = innerHTMLstr;
pending…
100 element createElement added in sequence
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.innerHTML = i;
  div.appendChild(span);
  outerdiv.appendChild(div);

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

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

  span.innerHTML = i;
  div.appendChild(span);
  outerdiv.appendChild(div);

}

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:

1 comment

John Tantalo commented :

I removed jQuery library (not used) and modifie setup create the container element, assign it to a global, and then remove it on teardown.

Add a comment