innerHTML vs removeChild (v14)

Revision 14 of this benchmark created by Philip O'Brien on


Description

what method for empty a element is faster ?

Preparation HTML

<div id='box'></div>

Setup

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

Teardown


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

Test runner

Ready to run.

Testing in
TestOps/sec
removeFirstChild
while (box.firstChild) {
  box.removeChild(box.firstChild);
}
ready
innerHTML
box.innerHTML = '';
ready
removeLastChild
while (box.lastChild) {
  box.removeChild(box.lastChild);
}
ready

Revisions

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