innerHTML vs removeChild

JavaScript performance comparison

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

Info

what method for empty a element is faster ?

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

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in unknown unknown
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. Here’s a list of current revisions for this page:

0 comments

Add a comment