Document fragment vs html element append

JavaScript performance comparison

Revision 6 of this test case created by Rajesh Jagannathan

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 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…

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