Remove Elemens

Benchmark created by Edward on


Preparation HTML

<div id="data">
<ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
</ul>
<p>a</p>
<ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
</ul>
<p>b</p>
<ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
</ul>
<p>c</p>
<ul>
        <li>1</li>
        <li>2</li>
        <li>3</li>
</ul>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>

Test runner

Ready to run.

Testing in
TestOps/sec
Remove Via Tree Walker
function removeOtherElements( base, target ) {
  var
    rootElement = document.getElementsByTagName( base )[0],
    dest = target.toUpperCase(),
    walker = document.createTreeWalker( rootElement, NodeFilter.SHOW_ELEMENT, null, false ),
    node = walker.currentNode;

  for( var i = 0; i < node.children.length; i++ ) {
    if( node.children[i].nodeName !== dest ) {
      node.children[i].remove();
    }
  }
}

removeOtherElements( "div", "p" )
ready
Remove Via Jquery
$("#data").children().not("p").remove();
ready

Revisions

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

  • Revision 1: published by Edward on