DOM Removal

JavaScript performance comparison

Test case created by MhausT

Preparation code

<div id="container">
</div>
      
<script>
Benchmark.prototype.setup = function() {
  let container = document.getElementById("container");
  let docFragment = document.createDocumentFragment();
  for (let i = 0; i < 10000; ++i) {
  	let p = document.createElement('p');
  	for(let txtI = 0; txtI < 3; txtI++){
  		p.appendChild(document.createTextNode(`testing${txtI}`));
  	}
  	
  	docFragment.appendChild(p);
  }
  container.appendChild(docFragment);

};
</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
Remove-child-first-child
let container = document.getElementById("container");
while(container.firstChild) container.removeChild(container.firstChild);
pending…
Remove-child-last-child
let container = document.getElementById("container");
while(container.lastChild) container.removeChild(container.lastChild);
pending…
Remove-last-child
let container = document.getElementById("container");
while(container.lastChild) container.lastChild.remove();
pending…
InnerHTML
let container = document.getElementById("container");
container.innerHTML = '';
pending…
Range-delete-contents
let container = document.getElementById("container");
let range = new Range();
range.selectNodeContents(container );
range.deleteContents();
pending…
ReplaceWith-last-child
let container = document.getElementById("container");
while(container.lastChild) container.lastChild.replaceWith();
pending…
CloneNode
let container = document.getElementById("container");
let cloned = container.cloneNode(false);
container.replaceWith(cloned);
pending…

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

Compare results of other browsers

0 Comments