Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Empty div methods
(version: 0)
Comparing performance of:
removeChild (while firstChild) vs innerHTML vs replaceChildren vs removeChild (while lastChild) vs remove() while loop vs remove() forEach loop
Created:
3 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<div id='parent'> <li>home</li> <li>one</li> <li>two</li> <li>three</li> <li>four</li> <li>five</li> <li>six</li> <li>seven</li> <li>eight</li> </div>
Script Preparation code:
var parent = document.getElementById('parent');
Tests:
removeChild (while firstChild)
while (parent.firstChild) { parent.removeChild(parent.lastChild) }
innerHTML
parent.innerHTML = ""
replaceChildren
parent.replaceChildren()
removeChild (while lastChild)
while (parent.lastChild) { parent.removeChild(parent.lastChild); }
remove() while loop
while (parent.firstChild) { parent.firstChild.remove(); }
remove() forEach loop
[...parent.childNodes].forEach(el => el.remove())
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (6)
Previous results
Fork
Test case name
Result
removeChild (while firstChild)
innerHTML
replaceChildren
removeChild (while lastChild)
remove() while loop
remove() forEach loop
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's dive into the details of the benchmark. **What is being tested?** The provided JSON represents a set of JavaScript microbenchmarks that test various methods for removing child nodes from a DOM element, specifically an empty `<div>` with multiple `<li>` elements. The benchmarks are designed to measure the performance of different approaches to remove these nodes efficiently. **Options compared** The benchmark compares six different methods: 1. `removeChild` with a while loop (`while (parent.firstChild) { ... }`) 2. `innerHTML` set to an empty string (`parent.innerHTML = ""`) 3. `replaceChildren()` 4. `remove()` method with a while loop (`while (parent.lastChild) { ... }`) 5. `forEach` loop with the `remove()` method (`[...parent.childNodes].forEach(el => el.remove())`) 6. `remove()` method with a while loop on the last child node (`while (parent.lastChild) { ... }`) **Pros and Cons of each approach** 1. **`removeChild` with a while loop**: This method is straightforward but may be slow due to the overhead of repeatedly checking for the first child node. 2. **`innerHTML` set to an empty string**: This method directly updates the DOM, which can be efficient but may not be suitable for all use cases. 3. **`replaceChildren()`**: This method replaces all child nodes with a new set, which is more efficient than removing individual nodes. 4. **`remove()` method with a while loop on last child node**: Similar to the previous point, this method can be slow due to repeated checks. 5. **`forEach` loop with `remove()` method**: This approach uses an iterator to remove all child nodes, which is generally efficient but may have some overhead due to the use of an iterator. 6. **`remove()` method with a while loop on first child node**: Similar to the previous point, this method can be slow due to repeated checks. **Special considerations** The benchmark does not appear to include any special JavaScript features or syntax that would affect its behavior. **Other alternatives** There may be other approaches to removing child nodes from a DOM element that are not included in this benchmark. Some alternative methods could include: * Using the `DOMMutationObserver` API * Creating a new container element and appending it to the original element * Using a library or framework-specific method (e.g., React's `removeChild` function) Keep in mind that these alternatives may have different trade-offs in terms of performance, readability, and compatibility.
Related benchmarks:
parent contain child
parent vs document queryselectorAll
Empty an element
EventListener in parent vs EventListener in childs
Comments
Confirm delete:
Do you really want to delete benchmark?