Document fragment vs html element append

JavaScript performance comparison

Revision 3 of this test case created by Simo

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<ul id="container">
</ul>
<script>
Benchmark.prototype.setup = function() {
    var ITERATIONS = 1000;
    var container = document.getElementById("container");
};

Benchmark.prototype.teardown = function() {
    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
HTML element append
for (var i = 0; i < ITERATIONS; ++i) {
  container.appendChild(document.createElement("li"));
}
pending…
Fragment append
var frag = document.createDocumentFragment(),
    i;

for (i = 0; i < ITERATIONS; ++i) {
  frag.appendChild(document.createElement("li"));
}

container.appendChild(frag);
pending…
jQuery append
var $container = $(container),
    i;

for (i = 0; i < ITERATIONS; ++i) {
  $container.append(document.createElement("li"));
}
pending…
construct string + good ol' innerHTML
var lis = [];
for (var i = 0; i < ITERATIONS; ++i) {
  lis.push('<li></li>');
}

container.innerHTML = lis.join('\n');
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