innerHTML vs removeChild

JavaScript performance comparison

Revision 127 of this test case created

Info

what method for empty a element is faster ?

Preparation code

<div id='box'></div>
<script>
Benchmark.prototype.setup = function() {
    var b = document.getElementById('box');
    for (var i = 0; i < 1000; i++) {
      b.appendChild(document.createElement('div'));
    }
};

Benchmark.prototype.teardown = function() {
    box.textContent = '';
};
</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
textContent
box.textContent = '';
pending…
innerHTML
box.innerHTML = '';
pending…
removeChild/lastChild
while (box.lastChild) {
  box.removeChild(box.lastChild);
}
pending…
lastChild/remove
while (box.lastChild) {
  box.lastChild.remove;
}
pending…
removeChild/firstChild
while (box.firstChild) {
  box.removeChild(box.firstChild);
}
 
pending…
firstChild/remove
while (box.firstChild) {
  box.firstChild.remove();
}
 
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