<script
src="https://code.jquery.com/jquery-3.4.1.min.js"
integrity="sha256-CSXorXvZcTkaix6Yvo6HppcZGetbYMGWSFlBw8HfCJo="
crossorigin="anonymous"></script>
<div id="outer">
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div id="inner">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
<div id="outer2">
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div>
<div id="inner2">
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
var inner = document.getElementById("inner");
var outer = document.getElementById("outer");
var inner2 = document.getElementById("inner2");
var outer2 = document.getElementById("outer2");
var x = outer.contains(inner)
var y = outer.contains(inner2)
var z = outer2.contains(inner)
var a = outer2.contains(inner2)
function isDescendant(parent, child) {
var node = child.parentNode;
while (node != null) {
if (node == parent) {
return true;
}
node = node.parentNode;
}
return false;
}
var x = isDescendant(outer, inner);
var y = isDescendant(outer, inner2);
var z = isDescendant(outer2, inner);
var a = isDescendant(outer2, inner2);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
node.contains | |
node.parentNode |
Test name | Executions per second |
---|---|
node.contains | 1446545.0 Ops/sec |
node.parentNode | 616384.6 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks!
Benchmark Overview
The provided benchmark compares two approaches for checking if an element is contained within another element in a DOM tree: node.contains()
and node.parentNode
. The test case uses jQuery, a popular JavaScript library, to manipulate the HTML document.
What are we testing?
We're comparing the performance of these two approaches:
node.contains()
: This method checks if an element is contained within another element in the DOM tree.node.parentNode
: This method returns the parent node of an element and can be used to check if an element is a descendant of another element.Options Compared
The benchmark compares the performance of these two approaches:
node.contains()
node.parentNode
Pros and Cons of Each Approach
node.contains()
outer.contains(inner)
).node.parentNode
contains()
is not available or supported.Library: jQuery
In this benchmark, jQuery is used to manipulate the HTML document and create a nested DOM structure. Specifically, jQuery's document.getElementById()
method is used to retrieve elements by their IDs.
Special JS Feature/Syntax (None)
This benchmark does not use any special JavaScript features or syntax that would affect its performance or outcome.
Other Alternatives
If you're looking for alternative approaches or have specific requirements, consider the following:
Benchmark Preparation Code
The provided script preparation code creates two nested DOM structures with jQuery, defining inner
and outer
elements as child nodes of each other. The HTML preparation code includes the necessary HTML structure for these elements.
Individual Test Cases
Each test case defines a unique benchmark by modifying the node.contains()
or isDescendant(outer, inner)
function to generate specific input values (e.g., different element IDs or parent-child relationships).