innerHTML vs createElement test

JavaScript performance comparison

Revision 34 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
5000 element innerHTML with string concatenation
var innerHTMLstr = '<div>';
for (var i = 0; i < 5000; i++) {
  innerHTMLstr += '<div><span class="foo" style="color: red">' + i + '</span></div>';
}
innerHTMLstr += '</div>';

document.getElementById('container').innerHTML = innerHTMLstr;

document.title = document.getElementById('container').innerHTML;
pending…
5000 element innerHTML without string concatenation
var innerHTMLarr = ['<div>'];
for (var i = 0; i < 5000; i++) {
  innerHTMLarr.push('<div><span class="foo" style="color: red">');
  innerHTMLarr.push(i);
  innerHTMLarr.push('</span></div>');
}
innerHTMLarr.push('</div>');

document.getElementById('container').innerHTML = innerHTMLarr.join('');
document.title = document.getElementById('container').innerHTML;
pending…
5000 element createElement added in sequence
var container = document.getElementById('container');
var outerdiv = document.createElement('div');
container.appendChild(outerdiv);

for (var i = 0; i < 5000; i++) {
  var div = document.createElement('div');
  var span = document.createElement('span');
  span.setAttribute("class", "foo");
  span.setAttribute("style", "color: red");

  span.appendChild(document.createTextNode(i));
  div.appendChild(span);
  outerdiv.appendChild(div);

}

document.title = document.getElementById('container').innerHTML;
pending…
5000 element createElement bulk add
var container = document.getElementById('container');
var outerdiv = document.createElement('div');

for (var i = 0; i < 5000; i++) {
  var div = document.createElement('div');
  var span = document.createElement('span');
  span.setAttribute("class", "foo");
  span.setAttribute("style", "color: red");


  span.appendChild(document.createTextNode(i));
  div.appendChild(span);
  outerdiv.appendChild(div);

}

container.appendChild(outerdiv);

document.title = document.getElementById('container').innerHTML;
pending…
5000 element createElement parse each piece
var root, target;
target = document.getElementById('container');

for (var i = 0; i < 5000; i++) {
  root = document.createElement('div');
  root.innerHTML = '<div><span class="foo" style="color: red">' + i + '</span></div>';

  target.appendChild (root);
}


document.title = target.innerHTML;
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