innerHTML vs removeChild

JavaScript performance comparison

Revision 129 of this test case created

Info

what method for empty a element is faster ?

Preparation code

<div id='box'>
<a class="item_market_action_button item_market_action_button_green">
                                        <span class="item_market_action_button_edge item_market_action_button_left"></span>
                                        <span class="item_market_action_button_contents">
                                                Купить                                    </span>
                                        <span class="item_market_action_button_edge item_market_action_button_right"></span>
                                        <span class="item_market_action_button_preload"></span>
                                </a>
</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
        var tmp = document.getElementsByClassName('item_market_action_button_green');
        for (var i = 0; i < tmp.length; i++) {
          tmp[i].textContent = '';
        }
pending…
innerHTML
        var tmp = document.getElementsByClassName('item_market_action_button_green');
        for (var i = 0; i < tmp.length; i++) {
          tmp[i].innerHTML = '';
        }
pending…
removeChild/lastChild
        var tmp = document.getElementsByClassName('item_market_action_button_green');
        for (var i = 0; i < tmp.length; i++) {
          while (tmp[i].lastChild) {
            tmp[i].removeChild(tmp[i].lastChild);
          }
        }
pending…
lastChild/remove
var tmp = document.getElementsByClassName('item_market_action_button_green');
for (var i = 0; i < tmp.length; i++) {
  while (tmp[i].lastChild) {
    tmp[i].lastChild.remove();
  }
}
pending…
removeChild/firstChild
var tmp = document.getElementsByClassName('item_market_action_button_green');
for (var i = 0; i < tmp.length; i++) {
  while (tmp[i].firstChild) {
    tmp[i].removeChild(tmp[i].firstChild);
  }
}
pending…
firstChild/remove
var tmp = document.getElementsByClassName('item_market_action_button_green');
for (var i = 0; i < tmp.length; i++) {
  while (tmp[i].firstChild) {
    tmp[i].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