innerHTML vs removeChild

JavaScript performance comparison

Revision 14 of this test case created by Philip O'Brien

Preparation code

<div id='box'></div>
      
<script>
Benchmark.prototype.setup = function() {
  var box = document.getElementById('box');
  var innerBox1 = document.createElement('DIV');
  var innerBox2 = document.createElement('DIV');
  var innerBox3 = document.createElement('DIV');
  var innerBox4 = document.createElement('DIV');
  var innerBox5 = document.createElement('DIV');
  var innerBox6 = document.createElement('DIV');
  
  box.appendChild(innerBox1);
  box.appendChild(innerBox2);
  innerBox1.appendChild(innerBox3);
  innerBox1.appendChild(innerBox4);
  innerBox2.appendChild(innerBox5);
  innerBox2.appendChild(innerBox6);

};

Benchmark.prototype.teardown = function() {
  var innerBox1 = document.createElement('DIV');
  var innerBox2 = document.createElement('DIV');
  var innerBox3 = document.createElement('DIV');
  var innerBox4 = document.createElement('DIV');
  var innerBox5 = document.createElement('DIV');
  var innerBox6 = document.createElement('DIV');
  
  box.appendChild(innerBox1);
  box.appendChild(innerBox2);
  innerBox1.appendChild(innerBox3);
  innerBox1.appendChild(innerBox4);
  innerBox2.appendChild(innerBox5);
  innerBox2.appendChild(innerBox6);

};
</script>

Preparation code output

<div id='box'></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
removeFirstChild
while (box.firstChild) {
  box.removeChild(box.firstChild);
}
pending…
innerHTML
box.innerHTML = '';
pending…
removeLastChild
while (box.lastChild) {
  box.removeChild(box.lastChild);
}
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