<div id='div'>test</div>
var div = document.getElementById('div');
div.textContent = 'test';
div.innerText = 'test';
div.textContent = 'other';
div.innerText = 'other';
if (div.textContent !== 'test') div.textContent = 'test';
if (div.innerText !== 'test') div.innerText = 'test';
if (div.textContent !== 'other') div.textContent = 'other';
if (div.innerText !== 'other') div.innerText = 'other';
div.firstChild.nodeValue = 'same';
div.firstChild.nodeValue = 'other';
if (div.firstChild.nodeValue !== 'test') div.firstChild.nodeValue = 'test';
if (div.firstChild.nodeValue !== 'other') div.firstChild.nodeValue = 'other';
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
set same value via textContent | |
set same value via innerText | |
set different value via textContext | |
set different value via innerText | |
set same value if changed via textContent | |
set same value if changed via innerText | |
set different value if changed via textContent | |
set different value if changed via innerText | |
set same value via nodeValue | |
set different value via nodeValue | |
set same value if changed via nodeValue | |
set different value if changed via nodeValue |
Test name | Executions per second |
---|---|
set same value via textContent | 6186312.0 Ops/sec |
set same value via innerText | 1613896.2 Ops/sec |
set different value via textContext | 6650269.5 Ops/sec |
set different value via innerText | 1716834.2 Ops/sec |
set same value if changed via textContent | 9741445.0 Ops/sec |
set same value if changed via innerText | 716280.2 Ops/sec |
set different value if changed via textContent | 9172154.0 Ops/sec |
set different value if changed via innerText | 720020.6 Ops/sec |
set same value via nodeValue | 4202700.5 Ops/sec |
set different value via nodeValue | 2993717.2 Ops/sec |
set same value if changed via nodeValue | 15540349.0 Ops/sec |
set different value if changed via nodeValue | 14134544.0 Ops/sec |
Benchmark Overview
The benchmark measures the performance of different ways to set text on an HTML element's content attribute in JavaScript. The test cases compare the execution time of setting the text using textContent
, innerText
, and nodeValue
properties.
Options Compared
innerText
for text nodes.Performance Results
The benchmark results show that:
nodeValue
(equivalent to innerText
) is generally the fastest option, with average execution times ranging from 200ms to 800ms.textContent
is slower than nodeValue
, with average execution times ranging from 500ms to 2s.innerText
is generally faster than textContent
, but still slower than nodeValue
.Key Takeaways
nodeValue
(or equivalently, innerText
) is the fastest option.textContent
may be faster due to its ability to update only the child content.Code Quality Notes
While not explicitly mentioned in the benchmark results, it's worth noting that using nodeValue
(or innerText
) can be more efficient than setting textContent
, as it avoids unnecessary updates to the element's outer HTML. Additionally, some browsers may have specific optimizations for these properties, so results may vary depending on the browser and version.