Test name | Executions per second |
---|---|
replaceChildren | 480284.0 Ops/sec |
remove and insert | 681723.9 Ops/sec |
remove and replace | 362125.1 Ops/sec |
<div id="container1"></div>
<div id="container2"></div>
const container1 = document.querySelector('#container1');
const container2 = document.querySelector('#container2');
for (let i=0; i<300; i++) {
container1.appendChild(document.createElement('p'));
container2.appendChild(document.createElement('p'));
}
container1.replaceChildren(container2.children);
while (container1.firstChild)
container1.removeChild(container1.firstChild);
while (container2.firstChild)
container1.appendChild(container2.firstChild);
while (container1.firstChild)
container1.removeChild(container1.firstChild);
container1.replaceChildren(container2.children);