<script type="text/javascript" src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.1.0.min.js"></script>
//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;
}
var found = !!$innerOne.closest($body).length;
if(!found)throw new Error('BOOM');
var found = isOrIn($body[0],$innerOne[0]);
if(!found)throw new Error('BOOM');
var found = !!$innerOne.closest('body').length;
if(!found)throw new Error('BOOM');
var found = isOrIn('body',$innerOne[0]);
if(!found)throw new Error('BOOM');
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
!!closest.length | |
== and parentNodes | |
!!closest.length selector | |
== and parentNodes selector |
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 |
Let's dive into the explanation.
Benchmark Definition JSON:
The provided JSON defines a benchmark for testing different methods of checking whether an element is or is in another element. The script preparation code creates a large document fragment with multiple nested elements, and then defines two functions: isOrIn
(a custom function) and closest
. The HTML preparation code includes the jQuery library.
Function Being Tested:
The primary function being tested is isOrIn
, which takes two arguments: container
and contained
. It checks if contained
is inside or in any ancestor of container
.
Comparison Options:
===
(strict equality): Compares the types of both operands.==
(loose equality): Compares only the values, ignoring type differences.parentNodes
: Returns an array of all parent nodes of contained
.Pros and Cons:
===
(strict equality)
==
(loose equality)
parentNodes
Additional Considerations:
parentNodes
can be a good option to traverse up the DOM tree.===
may be more suitable.Other Alternatives: For checking if an element is inside or in any ancestor of another element, you could consider using:
contains()
: A jQuery method that checks if an element contains another element.hasChild()
: An alternative to ===
, it returns whether the current DOM node has a child matching a given selector.In summary, the choice of comparison option depends on your specific use case and requirements. If you need exact value comparison, ===
might be the best choice. However, if you need to check for containment or proximity in the DOM tree, parentNodes
could be a suitable alternative.
Libraries Used: