innerHTML vs createElement

JavaScript performance comparison

Revision 2 of this test case created

Preparation code

<div id="test"></div>
<script>
Benchmark.prototype.setup = function() {
    document.getElementById('test').innerHTML = '';
    var bspan = "";
   
    for (var i = 0; i < 10000; i++) {
      bspan += "<span>";
    }
    for (var i = 0; i < 10000; i++) {
      bspan += "</span>";
    }
};
</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
createElement
var el = document.createElement("div");

var lastSpan = document.createElement("span")
el.appendChild(lastSpan);
for (var i = 0; i < 10000; i++) {
  var span = document.createElement("span");
  lastSpan.appendChild(span);
  lastSpan = span;
}

document.getElementById("test").appendChild(el);
pending…
innerHTML
var el = document.createElement("div");

el.innerHTML = bspan;
document.getElementById("test").appendChild(el);
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