<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(!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 = $body.is($innerOne) || $.contains($body[0],$innerOne[0]);
var body = $body[0];
var innerOne = $innerOne[0];
var found = body==innerOne || $.contains(body,innerOne);
var found = !!$innerOne.closest($body).length;
var found = isOrIn($body[0],$innerOne[0]);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
is() or contains() | |
== or contains() | |
!!closest.length | |
== and parentNodes |
Test name | Executions per second |
---|---|
is() or contains() | 1061705.0 Ops/sec |
== or contains() | 2149648.8 Ops/sec |
!!closest.length | 617344.7 Ops/sec |
== and parentNodes | 3007692.2 Ops/sec |
Measuring the performance of different approaches to check if an element is or is contained within another in JavaScript can be a fascinating benchmarking exercise.
The provided JSON represents a benchmark test that evaluates four different methods:
is()
method: This approach uses the is()
function provided by jQuery, which checks if two elements are equal.contains()
method: This approach uses the contains()
function provided by jQuery, which checks if an element contains another element.==
: This approach directly compares two elements using the ==
operator to check for equality.closest()
method and !!
operator: This approach uses the closest()
method, which finds the closest ancestor of a given element that matches a specified selector, and then uses the !!
operator to check if the result is truthy.Now, let's analyze the pros and cons of each approach:
is()
method:is()
function in jQuery.contains()
method:contains()
function.==
:is()
function in jQuery.closest()
method and !!
operator:!!
.closest()
function and the !!
operator. Additionally, this approach may not work correctly for all cases, especially when dealing with complex element relationships or custom elements.In terms of performance, the optimized is()
function in jQuery is likely to be the fastest option, followed by direct comparisons using ==
. The contains()
method and the closest()
method + !!
operator approach are less efficient due to their additional overhead.
Other alternatives that could be considered for this benchmark include:
Element.contains()
method or Edge's contains()
function.Keep in mind that the performance results may vary depending on the specific use case and the characteristics of the input elements.