DOM insertion benchmarking

JavaScript performance comparison

Revision 7 of this test case created by ElliotB

Preparation code

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>

<div id='container'></div>
      
<script>
Benchmark.prototype.teardown = function() {
  var cont = document.getElementById('container');
  cont.innerHTML = "";

};
</script>

Preparation code output

<div id='container'></div>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
jQuery: DOM render in memory
var i = 20;
var aHTML = [];
while (i--) {
  aHTML.push("<div>a new div</div>");
}
var sHTML = aHTML.join("");
$('#container').append(sHTML);
pending…
jQuery: direct DOM insertion
var i = 20;
var domContainer = $('#container');
while (i--) {
  domContainer.append("<div>a new div</div>");
}
pending…
DOM cloneNode (includes text node)
var i = 20;
var div = document.createElement('div');
var text = document.createTextNode("a new div");
div.appendChild(text);
var frag = document.createDocumentFragment();
while (i--) {
  frag.appendChild(div.cloneNode(true));
}

var cnt = document.getElementById('container');
cnt.appendChild(frag);
pending…
DOM cloneNode and direct appendChild
var i = 20;
var div = document.createElement("div");
var cnt = document.getElementById('container');
div.innerHTML = "a new div";
while (i--) {
  cnt.appendChild(div.cloneNode(true));
};
pending…
InnerHTML on container
var i = 20;
var cnt = document.getElementById('container');
while (i--) {
  cnt.innerHTML += "<div>a new div</div>";
};
pending…
String via array.join() + innerHTML
var i = 20;
var cnt = document.getElementById('container');
var aHTML = [];
while (i--) {
  aHTML.push("<div>a new div</div>");
};
cnt.innerHTML = aHTML.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.

0 Comments