jQuery html() & text() vs native elem. Cached and non cached.

JavaScript performance comparison

Revision 18 of this test case created

Info

Testing DOM vs native element content with append and single substitutions. Also adding cached selectors so that the lookup isn't necessarily a factor just the function.

Preparation code

<script src="//code.jquery.com/jquery-git2.min.js"></script>

<div id="html"></div>
<div id="text"></div>
<div id="innerHTML"></div>
<div id="innerText"></div>
<div id="textContent"></div>
<script>
Benchmark.prototype.setup = function() {
    var elem = '';
    var len = 100;
};

Benchmark.prototype.teardown = function() {
    elem = '';
};
</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
$.html()
for (var i = 0; i < len; i++) {
  $("#html").html("More text");
}
pending…
$.text()
for (var i = 0; i < len; i++) {
  $("#text").text("More text");
}
pending…
el.innerHTML
for (var i = 0; i < len; i++) {
  document.getElementById('innerHTML').innerHTML = "More text";
}
pending…
el.innerText
for (var i = 0; i < len; i++) {
  document.getElementById('innerText').innerText = "More text";
}
pending…
el.textContent
for (var i = 0; i < len; i++) {
  document.getElementById('textContent').textContent = "More text";
}
pending…
$.html() Cached
elem = $("#html");

for (var i = 0; i < len; i++) {
  elem.html("More text");
}
pending…
$.text() Cached
elem = $("#text");

for (var i = 0; i < len; i++) {
  elem.text("More text");
}
pending…
el.innerHTML Cached
elem = document.getElementById('innerHTML');

for (var i = 0; i < len; i++) {
  elem.innerHTML = "More text";
}
pending…
el.innerText Cached
elem = document.getElementById('innerText');

for (var i = 0; i < len; i++) {
  elem.innerText = "More text";
}
pending…
el.textContent Cached
elem = document.getElementById('textContent');

for (var i = 0; i < len; i++) {
  elem.textContent = "More text";
}
pending…
$.prop innerText cached
elem = $("#innerText");

for (var i = 0; i < len; i++) {
  elem.prop("innerText", "More text");
}
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