var a = document.createElement('a');
var v;
v = a.textContent;
v = a.innerText;
v = a.innerHTML;
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
textContent | |
innerText | |
innerHTML |
Test name | Executions per second |
---|---|
textContent | 6152597.5 Ops/sec |
innerText | 5812785.0 Ops/sec |
innerHTML | 6952746.5 Ops/sec |
Let's break down the benchmark and explain what's being tested.
What is being tested?
The benchmark measures the performance differences between four ways to read text content from an HTML element in JavaScript:
textContent
innerText
(also known as innerText
)innerHTML
In other words, the benchmark compares how fast each of these methods can retrieve and return the text content of a given HTML element.
Options compared:
The four options being compared are:
textContent
: returns the text content of an element, without any markup (e.g., <script>...</script>
).innerText
: returns the text content of an element, including markup (e.g., <script>...</script>
, <span>...</span>
, etc.).innerHTML
: returns the HTML content of an element, including all child elements and their attributes.Pros and Cons:
Each method has its pros and cons:
textContent
:innerText
:textContent
, and includes unnecessary markup.innerHTML
:Other considerations:
textContent
, if you want to include certain elements or attributes in the returned text, you may need to use additional methods (e.g., querySelector()
or getAttribute()
).innerHTML
might be more convenient, but it's also slower and more memory-intensive.Library usage:
None of the tests appear to rely on a specific library. However, if we were to speculate, some libraries like jQuery or React might use similar methods (e.g., jq()
for textContent
or reactDom
for innerHTML
). But this is not explicitly stated in the benchmark.
Special JavaScript feature/syntax:
None of the tests appear to rely on special JavaScript features or syntax. However, if we consider modern JavaScript features like async/await or Promises, they might be used in the preparation code (e.g., creating an async function) but are not relevant to the performance comparison itself.
Alternative methods:
If you wanted to test other methods for reading text content from HTML elements, some alternatives could include:
getElementsByClassName()
or querySelectorAll()
with a regex pattern to extract specific text content.DOMParser
to parse an HTML string and extract text content.Keep in mind that these alternative methods might not be as optimized or efficient as the ones tested in the benchmark.