jQuery.html() vs jQuery.text() vs el.innerHTML vs el.innerText

JavaScript performance comparison

Revision 2 of this test case created

Info

Testing DOM vs native element content with append and single substitutions.

Preparation code

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

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

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
jQuery HTML
for (var i = 0; i < 50; i++) {
  $("#html").html("More text");
}
pending…
jQuery HTML cached
var content = $("#html");

for (var i = 0; i < 50; i++) {
  content.html("More text");
}
pending…
jQuery Text
for (var i = 0; i < 50; i++) {
  $("#text").text("More text");
}
pending…
jQuery Text cached
var content = $("#html");

for (var i = 0; i < 50; i++) {
  content.text("More text");
}
 
pending…
el.innerHTML
for (var i = 0; i < 50; i++) {
  document.body.children[3].innerHTML = "More text";
}
pending…
el.innerHTML cached
var content = document.body.children[3];

for (var i = 0; i < 50; i++) {
  content.innerHTML = "More text";
}
pending…
el.innerText
for (var i = 0; i < 50; i++) {
  document.body.children[3].innerText = "More text";
}
pending…
el.innerText cached
var content = document.body.children[3];

for (var i = 0; i < 50; i++) {
  content.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