Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Closest or loop
(version: 0)
Comparing performance of:
closest vs while
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div class="root"> <div> <div> <div> <div> <div> <div> <div> <div> <div class="foo"> <div> <div class="bar"></div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div> </div>
Tests:
closest
var element = document.querySelector(".bar"); var i = 1000; while (i--) { const res = element.closest(".foo"); if (res) { // do something } }
while
var element = document.querySelector(".bar"); var i = 1000; while (i--) { let el = element; while(el != null && !el.classList.contains("foo")) { el = el.parentElement; } if (el) { // do something } }
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
closest
while
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 and explain what's being tested, compared, and their pros/cons. **Benchmark Overview** MeasureThat.net provides a JavaScript microbenchmarking tool where users can create and run benchmarks to compare different approaches. The given benchmark tests two alternatives for finding an element within its ancestor hierarchy in HTML documents. **Script Preparation Code** The script preparation code is empty, which means that the benchmark doesn't require any initialization or setup before running the test cases. **Individual Test Cases** There are two test cases: 1. **Closest** ```javascript var element = document.querySelector(".bar"); var i = 1000; while (i--) { const res = element.closest(".foo"); // ... } ``` This approach uses `closest()` to find the closest ancestor of the `.bar` element that contains an element with class `foo`. The `while` loop runs 1000 times, and in each iteration, it checks if the result is not null. If it's not null, some code is executed. 2. **While** ```javascript var element = document.querySelector(".bar"); var i = 1000; let el = element; while (el != null && !el.classList.contains("foo")) { el = el.parentElement; } // ... ``` This approach uses a `while` loop to traverse the ancestor hierarchy of the `.bar` element until it finds an element with class `foo`. The loop runs 1000 times, and in each iteration, it updates the `el` variable. **Comparison** The benchmark compares the performance of these two approaches: * **Closest**: Uses `closest()` to find the closest ancestor. * **While**: Manually traverses the ancestor hierarchy using a `while` loop. **Pros/Cons** * **Closest**: + Pros: - Faster and more efficient, as it directly uses the browser's built-in `closest()` method. + Cons: - May not work in older browsers that don't support this method. * **While**: + Pros: - More portable across different browsers. + Cons: - Slower and less efficient, as it manually traverses the ancestor hierarchy. **Library** The `closest()` method is a part of the DOM API in modern browsers. Its purpose is to find the closest element within its ancestor hierarchy that matches a specified selector or CSS class. **Special JS Feature/Syntax** None mentioned explicitly, but note that this benchmark uses JavaScript's built-in `querySelector` and `closest` methods, as well as the `while` loop construct. **Other Alternatives** For similar benchmarks, you might also see approaches like: * Using `getElementsByClassName()` instead of `querySelector`. * Using ` Element.contains()` instead of `classList.contains()`. * Using CSS selectors with `:has()` pseudo-class. * Using library functions like jQuery's `.closest()` method. These alternatives would change the performance characteristics and may have different pros/cons compared to the original approaches.
Related benchmarks:
for loop tests
test class
While loop vs closest one of many on type/id/class
While loop parentElement vs closest (vanilla javascript)
Comments
Confirm delete:
Do you really want to delete benchmark?