innerHTML vs removeChild (v19)

Revision 19 of this benchmark created by Grant Kiely on


Description

what method for empty a element is faster ?

Preparation HTML

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

Setup

if(!document.getElementById('box')){
    var box = document.createElement('div');
    box.id = 'box';
    n.appendChild(box);
    }
    else{
    var box = document.getElementById('box');
    }
    
    box.innerHTML = '<h1>this is a heading<h1><div id="wrapper">Here is some cmmon content that you might find in a website</div><p>qesdafsdafdsa<p>testasingsda<p>tesa</p><h4>here is some more content<h4><h3>asdf</h3><div>more content</div><h1>this is a heading<h1><div id="wrapper">Here is some cmmon content that you might find in a website</div><p>qesdafsdafdsa<p>testasingsda<p>tesa</p><h4>here is some more content<h4><h3>asdf</h3><div>more content</div><h1>this is a heading<h1><div id="wrapper">Here is some cmmon content that you might find in a website</div><p>qesdafsdafdsa<p>testasingsda<p>tesa</p><h4>here is some more content<h4><h3>asdf</h3><div>more content</div><h1>this is a heading<h1><div id="wrapper">Here is some cmmon content that you might find in a website</div><p>qesdafsdafdsa<p>testasingsda<p>tesa</p><h4>here is some more content<h4><h3>asdf</h3><div>more content</div><h1>this is a heading<h1><div id="wrapper">Here is some cmmon content that you might find in a website</div><p>qesdafsdafdsa<p>testasingsda<p>tesa</p><h4>here is some more content<h4><h3>asdf</h3><div>more content</div><h1>this is a heading<h1><div id="wrapper">Here is some cmmon content that you might find in a website</div><p>qesdafsdafdsa<p>testasingsda<p>tesa</p><h4>here is some more content<h4><h3>asdf</h3><div>more content</div><h1>this is a heading<h1><div id="wrapper">Here is some cmmon content that you might find in a website</div><p>qesdafsdafdsa<p>testasingsda<p>tesa</p><h4>here is some more content<h4><h3>asdf</h3><div>more content</div><h1>this is a heading<h1><div id="wrapper">Here is some cmmon content that you might find in a website</div><p>qesdafsdafdsa<p>testasingsda<p>tesa</p><h4>here is some more content<h4><h3>asdf</h3><div>more content</div><h1>this is a heading<h1><div id="wrapper">Here is some cmmon content that you might find in a website</div><p>qesdafsdafdsa<p>testasingsda<p>tesa</p><h4>here is some more content<h4><h3>asdf</h3><div>more content</div><h1>this is a heading<h1><div id="wrapper">Here is some cmmon content that you might find in a website</div><p>qesdafsdafdsa<p>testasingsda<p>tesa</p><h4>here is some more content<h4><h3>asdf</h3><div>more content</div>'

Teardown


    if(!document.getElementById('box')){
    var box = document.createElement('div');
    box.id = 'box';
    n.appendChild(box);
    }
    else{
    var box = document.getElementById('box');
    }
    
    box.innerHTML = '<h1>this is a heading<h1><div id="wrapper">Here is some cmmon content that you might find in a website</div><p>qesdafsdafdsa<p>testasingsda<p>tesa</p><h4>here is some more content<h4><h3>asdf</h3><div>more content</div><h1>this is a heading<h1><div id="wrapper">Here is some cmmon content that you might find in a website</div><p>qesdafsdafdsa<p>testasingsda<p>tesa</p><h4>here is some more content<h4><h3>asdf</h3><div>more content</div><h1>this is a heading<h1><div id="wrapper">Here is some cmmon content that you might find in a website</div><p>qesdafsdafdsa<p>testasingsda<p>tesa</p><h4>here is some more content<h4><h3>asdf</h3><div>more content</div><h1>this is a heading<h1><div id="wrapper">Here is some cmmon content that you might find in a website</div><p>qesdafsdafdsa<p>testasingsda<p>tesa</p><h4>here is some more content<h4><h3>asdf</h3><div>more content</div><h1>this is a heading<h1><div id="wrapper">Here is some cmmon content that you might find in a website</div><p>qesdafsdafdsa<p>testasingsda<p>tesa</p><h4>here is some more content<h4><h3>asdf</h3><div>more content</div><h1>this is a heading<h1><div id="wrapper">Here is some cmmon content that you might find in a website</div><p>qesdafsdafdsa<p>testasingsda<p>tesa</p><h4>here is some more content<h4><h3>asdf</h3><div>more content</div><h1>this is a heading<h1><div id="wrapper">Here is some cmmon content that you might find in a website</div><p>qesdafsdafdsa<p>testasingsda<p>tesa</p><h4>here is some more content<h4><h3>asdf</h3><div>more content</div><h1>this is a heading<h1><div id="wrapper">Here is some cmmon content that you might find in a website</div><p>qesdafsdafdsa<p>testasingsda<p>tesa</p><h4>here is some more content<h4><h3>asdf</h3><div>more content</div><h1>this is a heading<h1><div id="wrapper">Here is some cmmon content that you might find in a website</div><p>qesdafsdafdsa<p>testasingsda<p>tesa</p><h4>here is some more content<h4><h3>asdf</h3><div>more content</div><h1>this is a heading<h1><div id="wrapper">Here is some cmmon content that you might find in a website</div><p>qesdafsdafdsa<p>testasingsda<p>tesa</p><h4>here is some more content<h4><h3>asdf</h3><div>more content</div>'
  

Test runner

Ready to run.

Testing in
TestOps/sec
removeChild
while (box.firstChild) {
  box.removeChild(box.firstChild);
}
ready
removeChild 2
while (box.lastChild) {
  box.removeChild(box.lastChild);
}
ready
innerHTML
box.innerHTML = '';
ready
removeEntire
var n = box.parentNode;
n.removeChild(box);
var box = document.createElement('div');
box.id = 'box';
n.appendChild(box);
 
ready

Revisions

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