<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 found = $body.is($innerOne) || $.contains($body[0],$innerOne[0]);
var body = $body[0]; var found = body==innerOne || $.contains(body,$innerOne[0]);
var found = !!$innerOne.closest($body).length;
var found = false;
var container = $body[0];
var contained = $innerOne[0];
if(contained===container){
found=true;
} else if(container && contained){
var pointer = contained;
var parent = pointer.parentNode;
while(parent && pointer!==container){
if(parent === container){
found=true;
break;
}
pointer = parent;
parent = pointer.parentNode;
}
}
--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() | 598321.6 Ops/sec |
== or contains() | 0.0 Ops/sec |
!!closest.length | 325614.9 Ops/sec |
== and parentNodes | 2093276.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks!
The provided JSON represents a benchmark test for comparing different methods of checking whether an element is contained within another element in jQuery.
Options being compared:
is()
methodcontains()
method (in jQuery 3.1.0)==
closest()
method and checking its lengthPros and cons of each approach:
is()
method: This is a built-in jQuery method that checks if an element is contained within another element. It's fast and efficient, but may not be as intuitive for developers who aren't familiar with it.contains()
method: This method was introduced in jQuery 3.1.0 and provides a more explicit way of checking containment. However, it may be slower than the is()
method due to its additional logic.==
: This approach is simple but can lead to performance issues if not implemented correctly (e.g., when dealing with complex DOM structures). It's also less efficient than using jQuery methods.closest()
method and checking its length: This approach uses a more modern JavaScript technique to find the closest ancestor element and then checks its length. While it may be faster for simple cases, it can lead to performance issues when dealing with deep DOM structures or complex comparisons.Library used:
The benchmark test uses jQuery 3.1.0, which is an older version of the library that still has some limitations compared to newer versions like jQuery 3.x or jQuery Sizzle.
Special JS feature or syntax:
None mentioned in this particular benchmark test. However, some future benchmarks might explore features like async/await, Promises, or modern JavaScript language features.
Other considerations:
document.createDocumentFragment()
and appending elements to it helps reduce DOM mutations and improves performance.Other alternatives:
If you're looking for alternative methods to check element containment, here are a few options:
querySelector()
and contains()
: This method is more explicit than the is()
method but may be slower due to its additional logic.forEach()
and checking childNodes: This approach iterates through the child nodes of an element to find containment, which can lead to performance issues with large DOM structures.Keep in mind that these alternatives may not be as efficient or intuitive as the compared methods in this benchmark test.