var a = document.createElement('a');
a.appendChild(document.createTextNode('text'));
a.textContent = 'text';
a.innerText = 'text';
a.innerHTML = 'text';
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
createTextNode | |
textContent | |
innerText | |
innerHTML |
Test name | Executions per second |
---|---|
createTextNode | 2187763.5 Ops/sec |
textContent | 6653291.0 Ops/sec |
innerText | 1911360.1 Ops/sec |
innerHTML | 963426.9 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Benchmark Overview
The benchmark is designed to compare four methods for setting text content in HTML elements: createTextNode
, textContent
, innerText
, and innerHTML
. The benchmark prepares an HTML element, a
, and then tests each method by setting the corresponding value to it.
Options Compared
document.createTextNode()
method and appends it to the HTML element.textContent
property.innerText
property.Pros and Cons
innerText
.textContent
, but with additional benefits for cases where only the inner text needs to be updated, like when using a rich text editor or displaying a preview of an input field.Library Usage
The document
object is used extensively in this benchmark, as it represents the global DOM (Document Object Model) and provides access to various methods for creating, manipulating, and querying HTML elements. The textContent
, innerText
, and innerHTML
properties are part of the HTMLElement
interface, which is a property of the document
object.
Special JavaScript Features
There are no special JavaScript features or syntax used in this benchmark that would be unfamiliar to most software engineers.
Other Alternatives
In terms of alternatives for setting text content, some other methods include:
In general, the choice of method depends on the specific requirements of your project, including performance considerations, memory usage constraints, and the need for advanced features like DOM manipulation and event handling.