Document fragment vs html element append

JavaScript performance comparison

Revision 8 of this test case created

Preparation code

<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 append
for (var i = 0; i < ITERATIONS; ++i) {
  var item = document.createElement("li");
  item.innerHTML = "Item:" + i;
  container.appendChild(item);
}
pending…
Fragment append
var frag = document.createDocumentFragment();

for (var i = 0; i < ITERATIONS; ++i) {
  var item = document.createElement("li");
  item.innerHTML = "Item:" + i;
  frag.appendChild(item);
}

container.appendChild(frag);
pending…
innerHTML
var html = "";
for (var i = 0; i < ITERATIONS; ++i) {
  html += "<li>" + "Item:" + i + "</li>";
}

container.innerHTML = html;
pending…
innerHTML join
var html = [];
for (var i = 0; i < ITERATIONS; ++i) {
  html.push("<li>" + "Item:" + i + "</li>");
}

container.innerHTML = html.join("");
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