appendChild vs. DocumentFragment vs. innerHTML

JavaScript performance comparison

Revision 42 of this test case created

Info

Comparing 3 different ways to dynamically insert new elements into the DOM tree.

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<div id="test1">
</div>
<script>
Benchmark.prototype.setup = function() {
   
   
};
</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
append string
$("#test1").append($("<p>").text("aaaaaa"));
$("#test1").empty();
pending…
append element
var p = $("#test1");
p.append($("<p>").text("aaaaaa"));
$("#test1").empty();
pending…
element append string
var div= $("#test1");
var p= $("p").text("aaaaaaa").eq(0);
div.append(p);

$("#test1").empty();
pending…
element append element
var div= $("#test1");
var p= $("p").filter(":first").text("aaaaaaa");
div.append(p);

$("#test1").empty();
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