HTML Preparation code:
AخA
 
1
<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.0.min.js"></script>
Script Preparation code:
x
 
//Add 100,000 elements
var frag = document.createDocumentFragment();
for (var i=0; i<10; i++){
  var outDiv = document.createElement('div');
  for (var j=0; j<100; j++){
    var midDiv = document.createElement('div');
    for (var k=0; k<100; k++){
      var inDiv = document.createElement('div');
      if(i==6 && j==60){
        if(k==60)
          inDiv.id="one";
        else if(k==61)
          inDiv.id="two";
      }
      midDiv.appendChild(inDiv)
    }
    outDiv.appendChild(midDiv)
  }
  frag.appendChild(outDiv);
}
document.body.appendChild(frag);
var $innerOne = $(document.getElementById('one'));
var $body = $(document.body)
var isOrIn = function isOrIn(container, contained){
  var found = false;
  if(typeof container === 'string'){
    if(!container || !contained){
    }else if($(contained).is(container)){
     found=true;
    } else{
      var pointer = contained;
      var parent = pointer.parentNode;
      while(parent){
        if($(parent).is(container)){
            found=true;
            break;
        }
        pointer = parent;
        parent = pointer.parentNode;
      }
    }
  }else{
    if(!container || !contained){
    }else if(contained===container){
     found=true;
    } else{
      var pointer = contained;
      var parent = pointer.parentNode;
      while(parent && pointer!==container){
        if(parent === container){
            found=true;
            break;
        }
        pointer = parent;
        parent = pointer.parentNode;
      }
    }
  }
  return found;
}
Tests:
  • !!closest.length

     
    var found = !!$innerOne.closest($body).length;
    if(!found)throw new Error('BOOM');
  • == and parentNodes

     
    var found = isOrIn($body[0],$innerOne[0]);
    if(!found)throw new Error('BOOM');
  • !!closest.length selector

     
    var found = !!$innerOne.closest('body').length;
    if(!found)throw new Error('BOOM');
  • == and parentNodes selector

     
    var found = isOrIn('body',$innerOne[0]);
    if(!found)throw new Error('BOOM');
Rendered benchmark preparation results:

Suite status: <idle, ready to run>

Previous results

Experimental features:

  • Test case name Result
    !!closest.length
    == and parentNodes
    !!closest.length selector
    == and parentNodes selector

    Fastest: N/A

    Slowest: N/A

Latest run results:
Run details: (Test run date: 7 years ago)
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36
Chrome 60 on Windows
View result in a separate tab
Test name Executions per second
!!closest.length 629035.4 Ops/sec
== and parentNodes 2960543.5 Ops/sec
!!closest.length selector 340532.5 Ops/sec
== and parentNodes selector 167793.9 Ops/sec