javascript remove all child nodes javascript / jquery (v222)

Revision 222 of this benchmark created on


Preparation HTML

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

<script>
var boxAnchor = document.createElement('div');
boxAnchor.style.cssText="white-space:nowrap";
boxAnchor.id = 'box';
for(var i=0;i<1000;i++)
{
boxAnchor.appendChild(document.createElement('B').insertBefore(document.createTextNode('asdf fewf')).parentNode);
}
var box=document.getElementById('box');
function refreshRange()
{
box.parentNode.replaceChild(boxAnchor.cloneNode(true),box);
box=document.getElementById('box');
}
var range = document.createRange();
refreshRange();

function sleep(mseconds) 
{
  var e = new Date().getTime() + (mseconds);
  while (new Date().getTime() <= e) {}
}

</script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

Setup

refreshRange();

Teardown



            refreshRange();
        
  

Test runner

Ready to run.

Testing in
TestOps/sec
removeChild Recheck
while (box.firstChild) 
  box.removeChild(box.firstChild);
ready
removeChild lastChild with reference
    var last;
    while ((last = box.lastChild)) box.removeChild(last);
ready
firstChild haschildnodes() check
while(box.hasChildNodes())
  box.removeChild(box.firstChild);
ready
deletecontents (IE9+)
range.selectNodeContents(box);
range.deleteContents();
ready
replaceChild cloneNode(shallow) (DESTRUCTIVE)
var newBox = box.cloneNode(false);
box.parentNode.replaceChild(newBox,box);
box=newBox;
ready
removeChild then reAttach
var parent = box.parentNode;
var next = box.nextSibling;
parent.removeChild(box);
while(box.hasChildNodes())
  box.removeChild(box.firstChild);
parent.insertBefore(box,next);
ready
innerhtml = ""
box.innerHTML = '';
ready
jquery
$(box).contents().remove();
ready
hide
var display = box.style.display;
box.style.display = 'none';
while(box.hasChildNodes())
  box.removeChild(box.firstChild);
box.style.display = display;
ready
jQuery Empty
$(box).empty();
ready
assert
var last = box.lastChild;
if (!last)
   sleep(300);
 while ((last = box.lastChild)) box.removeChild(last);
ready

Revisions

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