append vs. innerHTML vs. fragment

JavaScript performance comparison

Test case created by Lee Brimelow

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
      
<script>
Benchmark.prototype.setup = function() {
  var html = "<div><ul><li>df</li><li>fdf</li></ul></div>";
  var d = document.createElement('div');
  document.body.appendChild(d);

};

Benchmark.prototype.teardown = function() {
  document.body.removeChild(d);

};
</script>

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
jQuery append
$(d).append(html);
pending…
innerHTML
d.innerHTML = html;
pending…
fragment
var f = document.createDocumentFragment();
var di = document.createElement('div');
var ule = document.createElement('ul');
var li1 = document.createElement('li');
li1.textContent = "df";
ule.appendChild(li1);
var li2 = document.createElement('li');
li2.textContent = "fdf";
ule.appendChild(li2);
di.appendChild(ule);
f.appendChild(di);
d.appendChild(f);
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