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

JavaScript performance comparison

Revision 19 of this test case created by

Preparation code

<script src="https://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

<div id="html"></div> <div id="text"></div> <div id="innerHTML"></div> <div id="innerText"></div> <div id="textContent"></div>

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
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…
hybrid cached
elem = $("#innerText")[0];

for (var i = 0; i < len; i++) {
  elem.innerText = "More text";
}
pending…
set per
elem = $(".user-output div");

for (var i = 0; i < len; i++) {
  elem.css({width: 10, height:10});
}
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.

0 Comments