innerHTML vs createElement

JavaScript performance comparison

Test case created

Preparation code

<div id="test"></div>
<script>
Benchmark.prototype.setup = function() {
    document.getElementById('test').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
createElement
var el = document.createElement("div");

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

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

for (var i = 0; i < 10000; i++) {
  span += "<span></span>";
}

el.innerHTML = span;
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