Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
jr_test_contains_vs_closest
(version: 0)
which is faster: contains or closest?
Comparing performance of:
contains vs closest
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div id='div1'> <div id='div2'> <div id='div3'> </div> </div> </div>
Script Preparation code:
var box = document.getElementById('div1'); var clicked = document.getElementById('div3');
Tests:
contains
const isClickInside = box.contains(clicked); return isClickInside;
closest
const isClickInside = clicked.closest('#div1'); return isClickInside;
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:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark definition and test cases to understand what's being tested. **Benchmark Definition:** The website measures the performance of two methods: `contains()` and `closest()`. The `contains()` method checks if an element is contained within another element, while `closest()` finds the closest ancestor element that matches a specified selector. **Script Preparation Code:** ```javascript var box = document.getElementById('div1'); var clicked = document.getElementById('div3'); ``` This code retrieves two elements from the HTML page using their IDs: `box` (the outermost container) and `clicked` (a child element within `box`). **Html Preparation Code:** ```html <div id='div1'> <div id='div2'> <div id='div3'> </div> </div> </div> ``` This HTML code defines the structure of the page, where `div1` is the outermost container, and `div3` is a child element within it. **Individual Test Cases:** There are two test cases: ### contains ```javascript const isClickInside = box.contains(clicked); return isClickInside; ``` This code checks if `clicked` (the innermost element) is contained within `box` (the outermost container). The `contains()` method returns a boolean value indicating whether the element is found. ### closest ```javascript const isClickInside = clicked.closest('#div1'); return isClickInside; ``` This code uses the `closest()` method to find the closest ancestor element that matches the selector `#div1`. Since `div3` is already inside `div1`, this method will return a reference to itself. The result is then returned. **Pros and Cons:** * **contains():** * Pros: * Simple and intuitive * Works well for most cases * Cons: * Can be slower than `closest()` due to the overhead of checking each element in the DOM tree * **closest():** * Pros: * Often faster than `contains()` * Handles cases where the target element is not directly contained within another element * Cons: * More complex and may have a higher chance of errors **Library:** The `closest()` method uses the Web Content Accessibility Guidelines (WCAG) 2.1 specification, which defines an API for finding the closest ancestor element that matches a specified selector. **Special JS Feature/Syntax:** None mentioned in this benchmark definition. **Other Alternatives:** If you need to check if an element is contained within another, you can use: * `contains()` with the `Element.contains()` method (Chrome 114) * `closest()` with a more specific selector (e.g., `closest('#div1')`) Keep in mind that the performance difference between these methods may vary depending on your specific use case and the browser being used.
Related benchmarks:
classList.contains() vs closest() performance
contains vs closest1
contains vs closest (actually working benchmark)
Compare contains vs closest
Comments
Confirm delete:
Do you really want to delete benchmark?