append vs. innerHTML vs. fragment

JavaScript performance comparison

Revision 14 of this test case created by Raymond

Preparation code

<script src="//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');
    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);
   
    document.body.appendChild(d);
   
    var $d = $(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 unknown unknown
Test Ops/sec
jQuery append fragment
$(d).append(f);
pending…
innerHTML
d.innerHTML = html;
pending…
fragment
d.appendChild(f);
pending…
cached $d appendChild
$d[0].appendChild(f);
pending…
cached $d append
$d.append(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. Here’s a list of current revisions for this page:

0 comments

Add a comment