Comparison: innerHTML
vs removeChild()
vs remove()
https://piecioshka.github.io/innerhtml-vs-removechild-vs-remove/
Code below is execute 100_000 times.
innerHTML
:
$element.innerHTML = '';
removeChild()
:
while ($element.firstElementChild) {
$element.removeChild($element.firstElementChild);
}
remove()
:
while ($element.firstElementChild) {
$element.firstElementChild.remove();
}
Results:
[Element exists in DOM: innerHTML] 20 ms
[Element exists in DOM: removeChild()] 49 ms
[Element exists in DOM: remove()] 31 ms
[Element does not exists in DOM: innerHTML] 11 ms
[Element does not exists in DOM: removeChild()] 1 ms
[Element does not exists in DOM: remove()] 2 ms
The MIT License @ 2022