Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Recursion closest() vs loop closest()
(version: 0)
Comparing performance of:
Recursion vs Loop
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div class="parent1" data-test="true"> <div class="parent2"> <div class="parent3"> <div id="parent4"></div> </div> </div> </div>
Tests:
Recursion
const getOutside = (el, expr) => { return el && (expr) ? el : (el !== document) ? getOutside(el.parentNode, expr) : null; } var target = document.getElementById("parent4"); var test = getOutside(target, target.dataset.test);
Loop
const getOutside = (el, expr) => { // Traverse the DOM up with a while loop while (!expr) { // Increment the loop to the parent node el = el.parentNode; if (!el) { return null; } } // At this point, the while loop has stopped and `el` represents the element that has // the class you specified in the second parameter of the function `clazz` // Then return the matched element return el; } var target = document.getElementById("parent4"); var test = getOutside(target, target.dataset.test);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Recursion
Loop
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 benchmark and its test cases. **What is being tested:** The benchmark is designed to compare two approaches for finding an element in the DOM (Document Object Model): recursion and iteration (loop). In the first test case, "Recursion," the `getOutside` function uses a recursive approach to traverse the DOM up from the target element (`target`) until it finds an element that matches the given expression (`expr`). The function stops when it reaches the document root or finds a match. In the second test case, "Loop," the `getOutside` function uses a loop to traverse the DOM up from the target element. The loop continues until the expression is falsy, at which point it returns the matched element. **Options compared:** The two approaches being compared are: 1. **Recursion**: A top-down approach where the function calls itself recursively until it reaches the desired depth or finds a match. 2. **Iteration (Loop)**: A bottom-up approach where the function traverses the DOM up from the target element using a loop, incrementing the parent node until it finds a match. **Pros and Cons of each approach:** * **Recursion:** + Pros: - Can be more concise and elegant in some cases. - Can be easier to understand for developers familiar with recursive functions. + Cons: - Can lead to stack overflow errors if the recursion is too deep. - May have slower performance due to function call overhead. * **Iteration (Loop):** + Pros: - Typically faster and more efficient than recursive approaches. - Less prone to stack overflow errors. + Cons: - Can be less concise and more verbose than recursive approaches. **Library usage:** The `getOutside` function uses the following libraries/libraries-like constructs: * None explicitly, but it uses JavaScript's built-in DOM manipulation APIs (e.g., `document.getElementById`, `el.parentNode`) to traverse the DOM. **Special JS features or syntax:** There are no special JS features or syntax used in this benchmark. It only relies on standard JavaScript functions and libraries. **Other alternatives:** Alternative approaches for finding an element in the DOM could include: * Using a library like jQuery, which provides a more concise and expressive API for DOM manipulation. * Utilizing modern JavaScript techniques like `requestIdleCallback` or `Web Workers` to optimize performance. * Employing other iteration strategies, such as using a queue data structure to store elements to be processed. Keep in mind that the choice of approach depends on the specific requirements of your use case, including factors like performance, conciseness, and maintainability.
Related benchmarks:
Benchmark: Parent vs Closest 2
Fastest way to list children: childNodes vs children vs firstChild/nextSibling vs firstElementChild/nextElementSibling v2 fixed
childNodes vs children vs firstChild/nextSibling vs firstElementChild/nextElementSibling (optimized html)
While loop parentElement vs closest (vanilla javascript)
Comments
Confirm delete:
Do you really want to delete benchmark?