innerHTML vs. cloneNode
JavaScript performance comparison
Info
Tests various DOM construction patterns.
Preparation code
<style>
.destination {display: none;}
</style>
<div id="destination1" class="destination"></div>
<div id="destination2" class="destination"></div>
<div id="destination3" class="destination"></div>
<div id="destination4" class="destination"></div>
<div id="destination5" class="destination"></div>
<script>
var content = '<div class="test"><span style="color: red">Wooo Text!</span></div><div>More text!</div>',
dest1 = document.getElementById("destination1"),
dest2 = document.getElementById("destination2"),
dest3 = document.getElementById("destination3"),
dest4 = document.getElementById("destination4"),
dest5 = document.getElementById("destination5");
// For Element cloneNode test
var womb = document.createElement("div");
womb.innerHTML = content;
// For doc fragment test
var frag = document.createDocumentFragment(),
clone = womb.cloneNode(true),
len = clone.childNodes.length;
while (len--) {
frag.appendChild(clone.childNodes[0]);
}
</script>
Preparation code output
Test runner
Warning! For accurate results, please disable Firebug before running the tests. (Why?)
Java applet disabled.
| Test | Ops/sec | |
|---|---|---|
innerHTML |
|
pending… |
cloneNode |
|
pending… |
DocumentFragment |
|
pending… |
DOM post insert |
|
pending… |
DOM pre insert |
|
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:
- Revision 1: published by Kevin Decker and last updated
- Revision 2: published by aavezel
0 comments