innerHTML vs innerText vs nodeValue vs textContent

JavaScript performance comparison

Revision 34 of this test case created

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() {
    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;
};
</script>

Preparation code output

x
x

Test runner

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

Java applet disabled.

Testing in unknown unknown
Test Ops/sec
innerText - div
a.innerText = s;
pending…
innerText - span
b.innerText = s;
pending…
innerText - label
c.innerText = s;
pending…
innerHTML - div
a.innerHTML = s;
pending…
innerHTML - span
b.innerHTML = s;
pending…
innerHTML - label
c.innerHTML = s;
pending…
textContent - div
a.textContent = s;
pending…
textContent - span
b.textContent = s;
pending…
textContent - label
c.textContent = s;
pending…
nodeValue - div
an.nodeValue = s;
pending…
nodeValue - span
bn.nodeValue = s;
pending…
textContent via function
set_text(a, s);
pending…
innerText 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 (innerTextIsBetter) {
   a.innerText = 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. Here’s a list of current revisions for this page:

0 comments

Add a comment