<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]);
if(!found)throw new Error('BOOM');
var body = $body[0];
var innerOne = $innerOne[0];
var found = body==innerOne || $.contains(body,innerOne);
if(!found)throw new Error('BOOM');
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');
--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() | 2832116.5 Ops/sec |
== or contains() | 5409927.5 Ops/sec |
!!closest.length | 1617074.8 Ops/sec |
== and parentNodes | 7420063.0 Ops/sec |
Let's break down the benchmark and its options.
Benchmark Description
The benchmark tests different methods for checking whether an element is or is contained within another element. The test starts with jQuery objects and uses ===
(strict equality) or parentNodes
as function calls to check the relationship between elements.
Options Compared
The benchmark compares four different approaches:
is()
method: A built-in jQuery method that checks if an element is contained within another element..contains()
method: Another jQuery method, now deprecated in favor of includes()
(more on this later), but still supported for backward compatibility.===
: Directly comparing two elements using the strict equality operator (===
)..closest()
method: A jQuery method that finds the closest ancestor element containing a given element.Pros and Cons of Each Approach
is()
method:display: none
or visibility: hidden
..contains()
method:===
:.closest()
method:Library and Its Purpose
The benchmark uses jQuery, a popular JavaScript library that simplifies DOM manipulation and event handling. jQuery provides various methods, including is()
, .contains()
, and .closest()
, which are used in the benchmark to compare different approaches for checking element relationships.
Special JS Features or Syntax
None of the benchmark options use special JavaScript features or syntax beyond what's already explained.