<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.innerHTML = '';
var node = document.getElementById('container');
while(node.firstChild) node.removeChild(node.firstChild)
var node = document.getElementById('container');
while(node.firstChild) node.firstChild.remove()
var node = document.getElementById('container');
node.innerText = '';
var node = document.getElementById('container');
node.textContent = '';
var node = document.getElementById('container');
node.replaceChildren()
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
innerHTML | |
removeChild | |
remove | |
innerText | |
textContent | |
replaceChildren |
Test name | Executions per second |
---|---|
innerHTML | 2814145.5 Ops/sec |
removeChild | 4914551.5 Ops/sec |
remove | 4944070.0 Ops/sec |
innerText | 3686908.2 Ops/sec |
textContent | 2817907.0 Ops/sec |
replaceChildren | 2743750.8 Ops/sec |
The benchmark tested in this JSON involves comparing various methods for clearing or removing child elements from a DOM node in JavaScript, specifically checking the performance of different approaches when dealing with a specific <div>
container filled with 1,000 child <div>
elements.
innerHTML:
node.innerHTML = '';
node
to an empty string. This effectively removes all child elements.removeChild:
while(node.firstChild) node.removeChild(node.firstChild);
node
until there are no more children left.remove:
while(node.firstChild) node.firstChild.remove();
removeChild
, this method uses the remove()
method on each child node to remove it from the DOM.removeChild
, depending on the number of elements and layout complexity.innerText:
node.innerText = '';
innerHTML
, it can be less efficient for simply clearing DOM elements compared to other methods.textContent:
node.textContent = '';
innerText
as it does not involve layout recalculations for hidden elements.replaceChildren:
node.replaceChildren();
Based on the benchmark results obtained from running these methods on a desktop Linux environment with Chrome 129:
remove
method was the best performer, followed closely by removeChild
. Both methods efficiently operated on child nodes without excessive reflow.innerText
and textContent
provided competitive yet relatively slower performance compared to remove
and removeChild
.innerHTML
and replaceChildren
were at the lower end of the performance spectrum, mainly due to the overhead of parsing or handling layout changes.When choosing a method to clear child nodes, you may consider:
remove
and removeChild
might be preferred. For readability or ease of use, innerHTML
might be a valid choice, though it's essential to be aware of potential drawbacks.replaceChildren
which may not be supported in older browsers..empty()
, providing a simpler API for common operations like clearing a node's contents, albeit potentially at a performance cost.Overall, testing different methods based on the specific scenario and understanding their implications is key for making informed choices in JavaScript DOM manipulation tasks.