innerHTML vs innerText vs nodeValue vs textContent (v34)

Revision 34 of this benchmark created on


Preparation HTML

<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>

Setup

var a = document.getElementById('divinputme'),
      b = document.getElementById('spaninputme'),
      c = document.getElementById('labelinputme');
  var s = '1,2,3,4';
  a.innerHTML = s;
  b.innerHTML = s;
  c.innerHTML = s;
  var an = a.firstChild,
      bn = b.firstChild,
      cn = c.firstChild;
  var ah = a.offsetHeight,
      bh = b.offsetHeight,
      ch = c.offsetHeight;
  var set_text = function (ep, t) {
     ep.textContent = t;
  }
  var set_text_2 = function (ep, t) {
     ep.innerText = t;
  }
  var set_text_3 = function (ep, t) {
     ep.innerHTML = t;
  }
  var textContentIsBetter = true;
  var innerTextIsBetter = true;
  var innerHTMLIsBetter = true;

Test runner

Ready to run.

Testing in
TestOps/sec
innerText - div
a.innerText = s;
ready
innerText - span
b.innerText = s;
ready
innerText - label
c.innerText = s;
ready
innerHTML - div
a.innerHTML = s;
ready
innerHTML - span
b.innerHTML = s;
ready
innerHTML - label
c.innerHTML = s;
ready
textContent - div
a.textContent = s;
ready
textContent - span
b.textContent = s;
ready
textContent - label
c.textContent = s;
ready
nodeValue - div
an.nodeValue = s;
ready
nodeValue - span
bn.nodeValue = s;
ready
textContent via function
set_text(a, s);
ready
innerText via function
set_text_2(a, s);
ready
innerHTML via function
set_text_3(a, s);
ready
if (for Chrome)
if (textContentIsBetter) {
   a.textContent = s;
}
else {
   a.innerHTML = s;
}
ready
if (for FF)
if (innerTextIsBetter) {
   a.innerText = s;
}
else {
   a.innerHTML = s;
}
ready
if (for IE)
if (innerHTMLIsBetter) {
   a.innerHTML = s;
}
else {
   a.textContent = s;
}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.