append vs. innerHTML vs. fragment

JavaScript performance comparison

Revision 2 of this test case created by gloomybrain

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');
    document.body.appendChild(d);
    var $d = $(d);
};

Benchmark.prototype.teardown = function() {
    document.body.removeChild(d);
};
</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
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. Here’s a list of current revisions for this page:

0 comments

Add a comment