innerHTML vs removeChild vs jquery.empty (v201)

Revision 201 of this benchmark created by Harald Glatt on


Description

What method for emptying an element is faster?

Preparation HTML

<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<div id='box'></div>

Setup

var box = document.getElementById('box');
    for (var i=0;i<1000;i++){
        var c = document.createElement('div');
        c.textContent = 'siodfj'+Date.now();
        box.appendChild(c);
    }

Test runner

Ready to run.

Testing in
TestOps/sec
removeChild[i]
for (var i = box.childNodes.length - 1; i >= 0; i--) {
  box.removeChild(box.childNodes[i]);
}
 
ready
removeChild while hasChildNodes
while (box.hasChildNodes()) {
  box.removeChild(box.firstChild);
}
 
ready
jQuery empty
$(box).empty()
ready
innerHTML
box.innerHTML = '';
ready
removeChild while firstChild
while (box.firstChild) {
  box.removeChild(box.firstChild);
}
 
ready

Revisions

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