innerHTML vs DOM methods

JavaScript performance comparison

Revision 2 of this test case created by Matt Ball

Info

See comments @ http://stackoverflow.com/questions/7099258

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<ol id="list">
</ol>
<script>
  var html = "";
  for (i = 0; i <= 2000; i++) {
    html += "<li><a href='#'>Hi</a></li>";
  }
 
  var list = document.getElementById("list");
</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
innerHTML
list.innerHTML = html;
pending…
DOM methods
var anchor;
var listElement;
for (var i = 0; i <= 2000; i++) {
  listElement = document.createElement("li");
  anchor = document.createElement("a");
  anchor.innerHTML = "Hi";
  anchor.href = "#";
  listElement.appendChild(anchor);
  list.appendChild(listElement);
}
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