Document fragment vs innerHTML vs looped appendChild

JavaScript performance comparison

Test case created by Thomas Lindstrøm

Preparation code

<div id="my-div">
</div>
      
<script>
Benchmark.prototype.setup = function() {
  var div = document.getElementById('my-div')

};

Benchmark.prototype.teardown = function() {
  while (div.firstChild) div.removeChild(div.firstChild);

};
</script>

Preparation code output

<div id="my-div"> </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
innerHTML
var i = 0;

while (i < 20) {
  div.innerHTML += '<li>This is my list item number ' + i + '</li>';
i++; }
pending…
Looped appendChild
var i = 0;

while (i < 20) {
  var el = document.createElement('li');
  el.innerText = 'This is my list item number ' + i;
  div.appendChild(el);
i++; }
pending…
Document fragment
var i = 0, fragment = document.createDocumentFragment();

while (i < 20) {
  var el = document.createElement('li');
  el.innerText = 'This is my list item number ' + i;
  fragment.appendChild(el);
i++; }

div.appendChild(fragment);
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