Fastest method of clearing all children from a DOM element

JavaScript performance comparison

Revision 468 of this test case created by Aaditya Taparia

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