var a = document.createElement('a');
a.appendChild(document.createTextNode('text'));
a.textContent = 'text';
a.innerText = 'text';
a.innerHTML = 'text';
a.nodeValue = 'text';
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
createTextNode | |
textContent | |
innerText | |
innerHTML | |
nodeValue |
Test name | Executions per second |
---|---|
createTextNode | 3966306.2 Ops/sec |
textContent | 19034154.0 Ops/sec |
innerText | 5859000.0 Ops/sec |
innerHTML | 2535886.2 Ops/sec |
nodeValue | 25098704.0 Ops/sec |
Let's break down the provided JSON data and explain what's being tested, compared, and their pros and cons.
Benchmark Definition
The createTextNode, textContent, innerText, innerHTML, nodeValue
benchmark tests the performance of different methods to set or get the text content of an HTML element. The script preparation code creates a new HTML anchor (<a>
) element named a
, which will be used as the test subject.
Individual Test Cases
Each test case defines a specific method for setting or getting the text content of the a
element:
createTextNode
: Creates a new textNode
object using the document.createTextNode()
method and appends it to the a
element.textContent
: Sets the textContent
property of the a
element to a string value ('text'
).innerText
: Sets the innerText
property of the a
element to a string value ('text'
).innerHTML
: Sets the innerHTML
property of the a
element to a string value ('text'
).nodeValue
: Sets the nodeValue
property of the a
element to a string value ('text'
).Comparison
The benchmark compares the performance of these five methods:
createTextNode
textContent
innerText
innerHTML
nodeValue
Pros and Cons
Here's a brief analysis of each method:
createTextNode: Creates a new text node, which can be less efficient than setting the textContent
property directly.
textContent: Sets the text content of the element using its own property.
innerText: Similar to textContent
, but only applies to elements that support innerText (e.g., some text editors).
textContent
for certain use cases.innerHTML: Sets the HTML content of the element using its own property.
nodeValue: Sets the nodeValue
property of the element, which is similar to textContent
.
textContent
.Library Usage
There are no explicit libraries used in this benchmark. However, it's worth noting that some of these methods rely on the browser's internal implementation or standards (e.g., textContent
and innerText
). Other libraries might provide additional features or optimizations for specific scenarios.
Special JS Features or Syntax
None of the test cases explicitly use special JavaScript features or syntax. They focus solely on the DOM manipulation APIs provided by modern browsers.
Alternatives
If you need to compare performance in other contexts, consider the following alternatives:
Keep in mind that this specific benchmark is designed to compare the performance of simple DOM manipulation APIs, which might not be representative of real-world use cases.