innerHTML vs removeChild (v129)

Revision 129 of this benchmark created on


Description

what method for empty a element is faster ?

Preparation HTML

<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>

Setup

var b = document.getElementById('box');
    for (var i = 0; i < 1000; i++) {
      b.appendChild(document.createElement('div'));
    }

Teardown


    box.textContent = '';
  

Test runner

Ready to run.

Testing in
TestOps/sec
textContent
        var tmp = document.getElementsByClassName('item_market_action_button_green');
        for (var i = 0; i < tmp.length; i++) {
          tmp[i].textContent = '';
        }
ready
innerHTML
        var tmp = document.getElementsByClassName('item_market_action_button_green');
        for (var i = 0; i < tmp.length; i++) {
          tmp[i].innerHTML = '';
        }
ready
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);
          }
        }
ready
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();
  }
}
ready
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);
  }
}
ready
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();
  }

}
ready

Revisions

You can edit these tests or add more tests to this page by appending /edit to the URL.