This blog post suggests that textContent
is preferable to innerText
for avoiding layout thrashing. But it is focused on retrieving an element's text; for setting element text, the opposite appears to be true -- at least in the following example.
This example uses innerText
and produces no layout thrashing:
<style>
#test {
background-color: blue;
float: right;
width: 100px;
height: 100%;
}
</style>
Test test test
<div id="test"></div>
<script>
setInterval(function() {
document.querySelector('#test').innerText = 'innerText';
}, 100);
</script>
But replace innerText
with textContent
and watch it thrash:
Can someone explain this behavior? What can I do to avoid layout thrashing and still change an element's text in a standards-based way?
Definition and Usage The textContent property sets or returns the text content of the specified node, and all its descendants.
textContent gets the content of all elements, including <script> and <style> elements. In contrast, innerText only shows "human-readable" elements. textContent returns every element in the node. In contrast, innerText is aware of styling and won't return the text of "hidden" elements.
You are correct! Just like you observed. Setting textContent
does cause thrashing.
Here is what the DOM specification has to say:
textContent of type DOMString, introduced in DOM Level 3
This attribute returns the text content of this node and its descendants. When it is defined to be null, setting it has no effect. On setting, any possible children this node may have are removed and, if it the new string is not empty or null, replaced by a single Text node containing the string this attribute is set to.
A non thrashing way would be to get the element's text nodes and modify those instead of using textContent
or innerText
(which is non standard).
var test = document.getElementById("test");
var a = document.createTextNode("");
test.appendChild(a);
setInterval(function(){
a.nodeValue = "Test test test";
},100);
Of course if the actual text will change, a paint will have to occur to update what you're seeing.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With