Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Node closest vs node contains forkedd
(version: 0)
Comparing performance of:
Contains vs Closest
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div class="parent"> <div> <div> <div> <div> <div> <div> <div> <div> <div> <div class="child"> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div>
Script Preparation code:
var parent = document.querySelector(".parent"); var child = document.querySelector(".child");
Tests:
Contains
var x = parent.contains(child)
Closest
var x = child.closest('.parent')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Contains
Closest
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
2 years ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36
Browser/OS:
Chrome 120 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Contains
3736097.2 Ops/sec
Closest
1572808.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided JSON data and explain what's being tested in this JavaScript microbenchmark. **Benchmark Overview** The benchmark, named "Node closest vs node contains forkedd", tests two different methods to find an ancestor element within a DOM tree: `contains()` and `closest()`. These methods are used to traverse the DOM and find elements by navigating up the tree from a given element. **Options Compared** The two options being compared are: 1. `contains()`: This method returns a boolean value indicating whether the specified element is contained within another element. 2. `closest()`: This method returns the closest ancestor element that matches the specified selector. **Pros and Cons of Each Approach** * **`contains()`**: + Pros: - Simple to implement, as it only requires checking if one element contains another using the `includes()` or `forEach()` methods. + Cons: - Can be slower due to the potential need for multiple checks (e.g., checking if an element is contained within a parent and then a grandparent). * **`closest()```**: + Pros: - More efficient, as it only requires navigating up the DOM tree until a matching ancestor element is found. + Cons: - May require additional libraries or polyfills to support the `closest()` method. **Library and Syntax Used** In this benchmark, the `closest()` method is used from the jQuery library. The purpose of jQuery is to simplify DOM manipulation and provide a consistent way to interact with web pages. In this case, it's used to find the closest ancestor element that matches a given selector. **Special JS Features or Syntax** None are mentioned in this benchmark. **Other Alternatives** If `closest()` is not supported by all browsers (or libraries), alternative methods can be used: * `parentElement` property: This can be used to access the parent element of an HTML element. * `parentNode`: This returns the parent node of a given DOM element. While these alternatives exist, they may require additional code or library support to work consistently across all browsers and environments. **Benchmark Preparation Code** The provided preparation code generates the necessary HTML structure with a deeply nested DOM tree, which is used as the test subject for both `contains()` and `closest()`. This structure allows for a more realistic representation of how these methods are typically used in real-world applications. In summary, this benchmark tests two common methods for finding ancestor elements within a DOM tree: `contains()` and `closest()`. The results will provide insight into the relative performance of each approach on various browsers and devices.
Related benchmarks:
Node closest vs node contains
parentNode vs firstChild vs nextSibling vs previousSibling (plus bonus: firstChild.nextSibling, querySelectorAll)
Node parent walking vs node contains
Node parent walking vs node contains 2
Comments
Confirm delete:
Do you really want to delete benchmark?