<div id='hello'>Hello, World!</div>
let el = document.querySelector("#hello");
let i = 1000;
let el = document.querySelector("#hello");
while (i--) {
el.textContent = "Goodbye, cruel world!"
}
let i = 1000;
let el = document.querySelector("#hello");
while (i--) {
el.innerHtml = "Goodbye, cruel world!"
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
textContent | |
innerHtml |
Test name | Executions per second |
---|---|
textContent | 12112.8 Ops/sec |
innerHtml | 1272247.5 Ops/sec |
Benchmark Explanation
The provided JSON represents a JavaScript microbenchmark test case on MeasureThat.net. The benchmark compares the performance of two approaches: using innerHTML
and textContent
to update the text content of an HTML element.
Options Compared
Two options are being compared:
Pros and Cons
Library Used
In the provided benchmark code, no specific JavaScript library is used. However, document.querySelector
is used to select an HTML element, which suggests that a modern browser's DOM API is being utilized.
Special JS Feature or Syntax
None of the special features or syntax are mentioned in the benchmark definition.
Other Alternatives
There are other alternatives for updating text content in JavaScript, such as:
innerText
(supported by Internet Explorer and some older browsers)Benchmark Preparation Code
The provided script preparation code creates an HTML element with the ID "hello" and assigns its content using both innerHTML
and textContent
. The HTML preparation code sets up the basic HTML structure.
Individual Test Cases
Each test case runs a loop that updates the text content of the selected element using either innerHTML
or textContent
. The loop iterates 1000 times, which is likely chosen to demonstrate performance differences between the two approaches.