Fastest method of clearing all children from a DOM element

JavaScript performance comparison

Revision 463 of this test case created by T. H. Doan

Preparation code

<div id="container"></div>
      
<script>
Benchmark.prototype.setup = function() {
  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');

};
</script>

Preparation code output

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

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
innerHTML = ''
box.innerHTML = '';
pending…
removeChild(box.firstChild)
while (box.firstChild) {
  box.removeChild(box.firstChild);
}
pending…
cloneNode(false)
var clone = box.cloneNode(false);
box.parentNode.replaceChild(clone, box);
pending…
removeChild(box.lastChild)
while (box.lastChild) {
  box.removeChild(box.lastChild);
}
pending…
children[0].remove()
while (box.children.length) {
  box.children[0].remove();
}
pending…

Compare results of other browsers

Revisions

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

0 Comments