jQuery html() versus appendChild

JavaScript performance comparison

Test case created by nwellcome

Info

This is to test if appending a giant string using $().html() is faster than creating the nodes manually.

Preparation code

<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js">
</script>
<ol id="list">
</ol>

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
Append a giant string
var html = "";
for (i = 0; i <= 2000; i++) {
  html += "<li><a href='#'>Hi</a></li>";
}
$("#list").html(html);
pending…
Append elements
var list = document.getElementById("list");
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