<div id="container"></div>
var node = document.getElementById('container');
for(var i = 0; i < 1000; i++) node.appendChild(document.createElement('div'));
var node = document.getElementById('container');
node.textContent = '';
var node = document.getElementById('container');
node.innerHTML = '';
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
textc | |
innerh |
Test name | Executions per second |
---|---|
textc | 2258312.8 Ops/sec |
innerh | 2581160.8 Ops/sec |
Let's dive into the provided benchmark test.
What is being tested?
The benchmark tests two approaches to clearing text content of an HTML element: textContent
and innerHTML
. The test creates 1000 HTML elements in a container, which are then cleared using these two methods. The goal is to determine which method is faster.
Options compared:
textContent
: This property sets or returns the text content of an element without changing its structure.innerHTML
: This property sets or returns the HTML content of an element, including elements and their attributes.Pros and Cons:
innerHTML
: Pros:Library used:
In this test, no specific library is mentioned. However, it's likely that the standard JavaScript DOM API is being used.
Special JS feature/syntax:
None are explicitly mentioned in this test case. If there were any special features or syntax, they would be relevant to the results and conclusions drawn from the benchmark.
Benchmark preparation code:
The script prepares a container element by appending 1000 HTML elements using appendChild
. The resulting HTML structure is then cleared using either textContent
or innerHTML
.
Html preparation code:
A simple HTML element (<div>
) with an ID of "container" is created and served as the test data.
Benchmark results:
The benchmark results show that textContent
is faster than innerHTML
. The exact execution times are provided, which can be used to inform future optimizations or implementations.
Other alternatives:
Some alternative approaches to clearing text content or HTML elements include:
display: none;
)element.replaceWith('')
Keep in mind that these alternatives might have different performance characteristics, depending on the specific use case and requirements.