Fastest method of clearing all children from a DOM element

JavaScript performance comparison

Revision 556 of this test case created by Rosberg Linhares

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
textContent
box.textContent = '';
pending…
removeChild(box.firstChild)
while (box.firstChild) {
  box.removeChild(box.firstChild);
}
pending…
remove()
while (box.firstChild) {
    box.firstChild.remove();
}
pending…
innerHTML = ''
box.innerHTML = '';
pending…
removeChild(box.lastChild)
while (box.lastChild) {
  box.removeChild(box.lastChild);
}
pending…

Revisions

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

0 Comments