innerHTML vs createElement test

JavaScript performance comparison

Revision 53 of this test case created

Preparation code

<script src="//code.jquery.com/jquery-git2.min.js"></script>

<div id="container"></div>
<script>
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 += '<div><span>' + i + '</span></div>';
}
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 div = document.createElement('div');
  var span = document.createElement('span');

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

}
pending…
100 element createElement bulk add - textnode
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.appendChild(document.createTextNode(i));
  div.appendChild(span);
  outerdiv.appendChild(div);

}

container.appendChild(outerdiv);
pending…
100 element createElement bulk add - textContent
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 = i;
  div.appendChild(span);
  outerdiv.appendChild(div);

}

container.appendChild(outerdiv);
pending…
100 element createElement bulk add - innertext
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.innerText = i;
  div.appendChild(span);
  outerdiv.appendChild(div);

}

container.appendChild(outerdiv);
pending…
Document Fragment
var outerDiv = document.createDocumentFragment();
var container = document.getElementById('container');


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

  span.innerText = i;
  div.appendChild(span);
  outerDiv.appendChild(div);

}

container.appendChild(outerDiv);
pending…
jQuery Append
var main = $('<div>');
for (var i = 0; i < 100; i++) {
  main.append('<div><span>' + i + '</span></div>');
}

document.getElementById('container').innerHTML = main;
pending…
jQuery Append clone
var main = $('<div>');
var span = $('<span>');
for (var i = 0; i < 100; i++) {
  span.clone().text(i);
  main.append(span)
}

document.getElementById('container').innerHTML = main;
pending…
jQuery Add
var main = $();
for (var i = 0; i < 100; i++) {
  main = main.add('<div><span>' + i + '</span></div>');
}

document.getElementById('container').innerHTML = main;
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