<div id='messages'></div>
const html = [];
for(i=0; i<1000; i++) {
html.push("<span>Text</span>");
}
document.getElementById("messages").insertAdjacentHTML('beforeend', html)
let list = document.getElementById("messages");
while (list.firstChild) {
list.removeChild(list.firstChild);
}
let list = document.getElementById("messages");
while (list.lastChild) {
list.removeChild(list.lastChild);
}
let list = document.getElementById("messages");
while (list.firstChild) {
list.firstChild.remove();
}
let list = document.getElementById("messages");
while (list.lastChild) {
list.lastChild.remove();
}
let list = document.getElementById("messages");
list.replaceChildren();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Remove firstChild | |
Remove lastChild | |
firstChild.remove | |
lastChild.remove | |
replaceChildren |
Test name | Executions per second |
---|---|
Remove firstChild | 60353228.0 Ops/sec |
Remove lastChild | 58729848.0 Ops/sec |
firstChild.remove | 40299172.0 Ops/sec |
lastChild.remove | 21240908.0 Ops/sec |
replaceChildren | 1614000.4 Ops/sec |
The benchmark defined in the provided JSON focuses on measuring the performance of different methods for removing child elements from a DOM element in JavaScript. The benchmark specifically tests five different approaches to removing child elements from the HTML <div id='messages'></div>
prepared for the test.
Remove firstChild using removeChild
:
let list = document.getElementById("messages");
while (list.firstChild) {
list.removeChild(list.firstChild);
}
Pros:
Cons:
removeChild()
can be relatively slower when removing many nodes due to repetitive function calls and potential reflows.Remove lastChild using removeChild
:
let list = document.getElementById("messages");
while (list.lastChild) {
list.removeChild(list.lastChild);
}
Pros:
firstChild
, this method is clear and easy to understand.Cons:
firstChild
, it suffers from the same performance drawbacks related to reflows when removing multiple nodes.Remove firstChild using remove
:
let list = document.getElementById("messages");
while (list.firstChild) {
list.firstChild.remove();
}
Pros:
remove()
method can be slightly more semantic, clearly indicating that you are removing the element itself.Cons:
removeChild()
, invoking remove()
also results in the same potential performance overhead.Remove lastChild using remove
:
let list = document.getElementById("messages");
while (list.lastChild) {
list.lastChild.remove();
}
Pros:
firstChild.remove()
example.Cons:
Use of replaceChildren
:
let list = document.getElementById("messages");
list.replaceChildren();
Pros:
Cons:
The benchmark results indicate that removing the last child and first child using removeChild
was the most efficient, achieving around 3.98 million executions per second and 3.96 million respectively. The replaceChildren
method, although conceptually superior for removing multiple elements, performed significantly slower in this specific test scenario with approximately 1.56 million executions per second.
Performance Implications: When dealing with a large number of child elements, looping through and removing them one by one can create performance bottlenecks due to the potential for multiple layout reflows. replaceChildren()
could be more optimal in a real-world scenario, especially if you need to clear all children.
Browser Compatibility: If targeting a wide range of browsers, using methods like removeChild()
may be more appropriate for compatibility, even though their performance may suffer when clearing many children repeatedly.
Alternative Approaches: Besides the methods tested, other considerations could include:
empty()
could be alternatives, although they might introduce their abstractions and performance characteristics.Choosing the appropriate method requires weighing performance implications against code clarity and browser compatibility based on the specific use case.