<div id='messages'></div>
let list = document.getElementById("messages");
for(i=0; i<10; i++) {
list.innerHTML += "<span>Text</span>"
}
let list = document.getElementById("messages");
while (list.firstChild) {
list.removeChild(list.firstChild);
}
let list = document.getElementById("messages");
while (list.lastChild) {
list.removeChild(list.lastChild);
}
document.getElementById("messages").innerHTML = "";
document.getElementById("messages").innerText = "";
document.getElementById("messages").textContent = "";
let list = document.getElementById("messages");
while (list.firstChild) {
list.firstChild.remove()
}
var range = new Range();
range.selectNodeContents(document.getElementById("messages"));
range.deleteContents();
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Remove firstChild | |
Remove lastChild | |
innerHTML | |
innerText | |
textContent | |
firstChild.remove | |
range |
Test name | Executions per second |
---|---|
Remove firstChild | 4683363.0 Ops/sec |
Remove lastChild | 4651363.0 Ops/sec |
innerHTML | 1979772.1 Ops/sec |
innerText | 2730578.5 Ops/sec |
textContent | 1781125.5 Ops/sec |
firstChild.remove | 4591531.5 Ops/sec |
range | 968372.1 Ops/sec |
Let's break down what's being tested in the provided JSON benchmarks.
Benchmark Overview
The tests aim to measure the performance of removing children from a DOM element, specifically using the innerHTML
, innerText
, and textContent
properties. The tests also compare the use of traditional removeChild()
method with modern alternatives like firstChild.remove()
and Range.deleteContents()
.
Options Compared
Here's a brief overview of the options being compared:
removeChild()
that targets only the first child node.Pros and Cons
Here's a brief summary of the pros and cons of each option:
innerHTML
since it only deals with text content.Library and Special JS Features
The test cases use the Range
API, which is a modern way of manipulating DOM content. The Range
API allows for efficient deletion of text and HTML contents from an element.
No special JavaScript features are explicitly mentioned in the benchmark code, but it's worth noting that some browsers may optimize or have specific behaviors for certain features.
Alternatives
If you're looking for alternative ways to remove children from a DOM element, consider using:
These alternatives may offer different performance characteristics or behaviors compared to the options being tested in the benchmark.