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

Revision 458 of this benchmark created by T. H. Doan on


Preparation HTML

<div id="box"></div>

Setup

var box = document.getElementById('box');
  box.innerHTML = '<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>';

Test runner

Ready to run.

Testing in
TestOps/sec
children[0].remove()
while (box.children.length) {
  box.children[0].remove();
}
ready
removeChild(box.lastChild)
while (box.lastChild) {
  box.removeChild(box.lastChild);
}
ready
cloneNode(false)
var clone = box.cloneNode(false);
box.parentNode.replaceChild(clone, box);
ready
removeChild(box.firstChild)
while (box.firstChild) {
  box.removeChild(box.firstChild);
}
ready
innerHTML = ''
box.innerHTML = '';
ready

Revisions

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