innerHTML vs innerText vs nodeValue vs textContent

JavaScript performance comparison

Revision 35 of this test case created by

Preparation code

<div id="divinputme" style="position:absolute;left:10px;top:0">x</div>
<span id="spaninputme" style="position:absolute;left:110px;top:0">x</span>
<label id="labelinputme" style="position:absolute;left:210px;top:0">x</label>
      
<script>
Benchmark.prototype.setup = function() {
  'use strict';
  var a = document.getElementById('divinputme'),
      b = document.getElementById('spaninputme');
  var s = '1,2,3,4';
  a.innerHTML = s;
  b.innerHTML = s;
  var an = a.firstChild,
      bn = b.firstChild;
  var ah = a.offsetHeight,
      bh = b.offsetHeight;
  var set_text = function (ep, t) {
     ep.textContent = t;
  }
  var set_text_2 = function (ep, t) {
     var n = ep.firstChild;
     if (n) {
        n.nodeValue = t;
     }
     else {
        n.appendChild(document.createTextNode(t));
     }
  }
  var set_text_3 = function (ep, t) {
     ep.innerHTML = t;
  }
  var textContentIsBetter = true;
  var nodeValueIsBetter = true;
  var innerHTMLIsBetter = true;

};
</script>

Preparation code output

<div id="divinputme" style="position:absolute;left:10px;top:0">x</div> <span id="spaninputme" style="position:absolute;left:110px;top:0">x</span> <label id="labelinputme" style="position:absolute;left:210px;top:0">x</label>

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
textContent - div
a.textContent = s;
pending…
innerHTML - div
a.innerHTML = s;
pending…
innerHTML - span
b.innerHTML = s;
pending…
innerText - div
a.innerText = s;
pending…
innerText - span
b.innerText = s;
pending…
textContent - span
b.textContent = s;
pending…
nodeValue - div
an.nodeValue = s;
pending…
nodeValue - span
bn.nodeValue = s;
pending…
textContent via function
set_text(a, s);
pending…
nodeValue via function
set_text_2(a, s);
pending…
innerHTML via function
set_text_3(a, s);
pending…
if (for Chrome)
if (textContentIsBetter) {
   a.textContent = s;
}
else {
   a.innerHTML = s;
}
pending…
if (for FF)
if (nodeValueIsBetter) {
   var n = a.firstChild;
   if (n) {
      n.nodeValue = s;
   }
   else {
      n.appendChild(document.createTextNode(s));
   }
}
else {
   a.innerHTML = s;
}
pending…
if (for IE)
if (innerHTMLIsBetter) {
   a.innerHTML = s;
}
else {
   a.textContent = s;
}
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

x
x