Emptify element

JavaScript performance comparison

Revision 5 of this test case created by Hupo

Preparation code

<div id="test-div"></div>
      
<script>
Benchmark.prototype.setup = function() {
  var i = 1000;
  var d = document;
  var div = d.getElementById('test-div');
  while(i--) {
    div.appendChild(d.createElement('span'));
    div.appendChild(d.createTextNode('test'));
  }
  
  var slice = [].slice;

};
</script>

Preparation code output

<div id="test-div"></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
div.innerHTML = null;
pending…
textContent
div.textContent = null;
pending…
firstChild
var node;
while (node = div.firstChild) {
  node.remove();
}
pending…
lastChild
var node;
while (node = div.lastChild) {
  node.remove();
}
pending…
Array from childNodes
var arr = slice.call(div.childNodes);
var i, len;
for (i = 0, len = arr.length; i < len; i ++) {
  arr[i].remove();
}
pending…
xpath
var node;
while (node = d.evaluate("child::*|text()", div, null, 8, null).singleNodeValue) {
  node.remove();
}
pending…
removeChild
while(div.hasChildNodes()) {
  div.removeChild(div.firstChild);
}
pending…
recreate
var parent = div.parentNode;
var newdiv = document.createElement('div');
newdiv.id = div.id;
parent.replaceChild(newdiv, div);
div = newdiv;
newdiv = null;
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