var span = document.createElement('span');
span.appendChild(document.createTextNode('Hello, World! 🥺'));
span.textContent = 'Hello, World! 🥺';
span.innerText = 'Hello, World! 🥺';
span.append('Hello, World! 🥺');
span.innerHTML = 'Hello, World! 🥺';
span.insertAdjacentHTML("beforeend", 'Hello, World! 🥺');
span.replaceChildren('Hello, World! 🥺');
span.appendChild(new Text('Hello, World! 🥺'));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
createTextNode | |
textContent | |
innerText | |
append | |
innerHTML | |
insertAdjacentHTML | |
replaceChildren | |
appendChild |
Test name | Executions per second |
---|---|
createTextNode | 3503840.5 Ops/sec |
textContent | 5691569.0 Ops/sec |
innerText | 2835451.5 Ops/sec |
append | 6739562.5 Ops/sec |
innerHTML | 5081645.5 Ops/sec |
insertAdjacentHTML | 1806851.8 Ops/sec |
replaceChildren | 4413955.0 Ops/sec |
appendChild | 4868496.5 Ops/sec |
The provided JSON represents a JavaScript microbenchmark test on MeasureThat.net, which compares the performance of different approaches for creating or manipulating text content in HTML elements.
What is tested?
The benchmark tests the following approaches:
createTextNode
: Creates a new text node using the document.createTextNode()
method.textContent
: Sets the text content of an element using the textContent
property.innerText
: Sets the inner text of an element using the innerText
property (which is not supported in all browsers, but used here for consistency).append
: Appends a string to the end of an element's content using the append()
method.innerHTML
: Sets the HTML content of an element using the innerHTML
property.insertAdjacentHTML
: Inserts HTML content into an element at a specified position using the insertAdjacentHTML()
method.replaceChildren
: Replaces the entire content of an element with new content using the replaceChildren()
method.appendChild
: Appends a child node to the end of an element's children using the appendChild()
method.Options compared:
Each approach is compared against others in terms of performance, specifically the number of executions per second.
Pros and cons:
Here are some pros and cons for each approach:
createTextNode
:textContent
:innerText
:append
:innerHTML
:insertAdjacentHTML
:replaceChildren
:Other considerations:
When choosing a method for creating or manipulating text content in HTML elements, consider factors such as:
innerText
.Alternatives:
Other alternatives to these approaches include:
Keep in mind that the choice of method depends on the specific requirements and constraints of your project, such as performance, security, and maintainability.