Test name | Executions per second |
---|---|
innerHTML | 2393813.8 Ops/sec |
replaceChildren | 1947323.5 Ops/sec |
remove by while | 22930084.0 Ops/sec |
removeChild by while | 19834692.0 Ops/sec |
remove by forEach | 1232102.6 Ops/sec |
<div id="parent"></div>
const node = document.getElementById('parent');
const child = document.createElement('div')
child.classList.add('child')
child.textContent = 'some text'
for(var i = 0; i < 5000; i++) node.appendChild(child);
const node = document.getElementById('parent');
node.innerHTML = '';
const node = document.getElementById('parent');
node.replaceChildren();
const node = document.getElementById('parent');
while(node.firstChild) node.firstChild.remove()
const node = document.getElementById('parent');
while(node.firstChild) node.removeChild(node.firstChild)
const node = document.querySelectorAll('.child');
for (const child of node) child.remove()