Fastest method of clearing all children from a DOM element (v474)

Revision 474 of this benchmark created by Md. Enzam Hossain on


Preparation HTML

<div id="container"></div>

Setup

document.getElementById('container').innerHTML = '<div id="box"><span>Abc123</span><span>Abc123</span><span>Abc123</span><span>Abc123</span><span>Abc123</span><span>Abc123</span><span>Abc123</span><span>Abc123</span><span>Abc123</span><span>Abc123</span><span>Abc123</span><span>Abc123</span><span>Abc123</span><span>Abc123</span><span>Abc123</span><span>Abc123</span><span>Abc123</span><span>Abc123</span><span>Abc123</span><span>Abc123</span><span>Abc123</span><span>Abc123</span><span>Abc123</span><span>Abc123</span><span>Abc123</span><span>Abc123</span></div>';
  var box = document.getElementById('box');

Test runner

Ready to run.

Testing in
TestOps/sec
removeChild(box.firstChild)
while (box.firstChild) {
  box.removeChild(box.firstChild);
}
ready
cloneNode(false)
box.replaceWith(box.cloneNode(false));
ready
innerText = ''
box.innerText = '';
ready
cachedLastChild
let child;
while (child = box.lastChild) {
  box.removeChild(child);
}
ready
removeChild(box.lastChild)
while (box.lastChild) {
  box.removeChild(box.lastChild);
}
ready
innerHTML = ''
box.innerHTML = '';
ready
removeChild(box.lastChild)
while (box.childNodes.length > 1) {
  box.removeChild(box.childNodes[box.childNodes.length - 2]);
}
box.removeChild(box.lastChild);
ready
textContent = '';
box.textContent = '';
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.