innerHTML vs innerText vs nodeValue vs textContent (v35)

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

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

Test runner

Ready to run.

Testing in
TestOps/sec
innerText - div
a.innerText = s;
ready
innerText - span
b.innerText = s;
ready
innerHTML - div
a.innerHTML = s;
ready
innerHTML - span
b.innerHTML = s;
ready
textContent - div
a.textContent = s;
ready
textContent - span
b.textContent = s;
ready
nodeValue - div
an.nodeValue = s;
ready
nodeValue - span
bn.nodeValue = s;
ready
textContent via function
set_text(a, s);
ready
nodeValue 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 (nodeValueIsBetter) {
   var n = a.firstChild;
   if (n) {
      n.nodeValue = s;
   }
   else {
      n.appendChild(document.createTextNode(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.