<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 innerOne = $innerOne[0];
var found = body==innerOne || $.contains(body,innerOne);
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() | 603026.6 Ops/sec |
== or contains() | 1184965.4 Ops/sec |
!!closest.length | 296822.9 Ops/sec |
== and parentNodes | 2168226.8 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks.
The provided JSON represents a benchmark test case on MeasureThat.net, which measures the performance of different methods for checking whether an element is or contains another element. The test creates a large HTML fragment with nested div elements and uses jQuery to manipulate them.
Benchmark Definition
The test has four individual test cases:
var found = $body.is($innerOne) || $.contains($body[0],$innerOne[0]);
$body
is an instance of the same class as $innerOne
, and also uses a global function .contains()
to check if $innerOne[0]
is contained within $body[0]
.var body = $body[0]; \r\nvar innerOne = $innerOne[0]; \r\nvar found = body==innerOne || $.contains(body,innerOne);
$body[0]
is equal to $innerOne[0]
, and also uses a global function .contains()
to check if $innerOne[0]
is contained within $body[0]
.var found = !!$innerOne.closest($body).length;
var found = false;\r\nvar container = $body[0];\r\nvar contained = $innerOne[0];\r\nif(contained===container){\r\n found=true;\r\n} else if(container && contained){\r\n var pointer = contained;\r\n var parent = pointer.parentNode;\r\n \r\n while(parent && pointer!==container){\r\n\tif(parent === container){\r\n\t\tfound=true;\r\n \tbreak;\r\n }\r\n pointer = parent;\r\n parent = pointer.parentNode;\r\n }\r\n \r\n}
$contained
is the same instance as $container
, and then uses a loop to find the closest ancestor that matches $container
.Options Compared
The test compares four different approaches:
is()
function and global .contains()
function.==
) and global .contains()
function.closest()
method.Pros and Cons
Here's a brief summary of each approach:
.contains()
function.Other Considerations
When working with HTML fragments, consider the following:
document.createDocumentFragment()
instead of document.createElement('div')
for performance reasons.By understanding these considerations and testing different approaches, you can make informed decisions about which method to use for your specific use case.