Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Closest vs Loop vs Recursion
(version: 0)
Comparing performance of:
Closest vs Loop vs Recursion
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<div class="parent"> <div class="child"> <div class="child"> <div class="child child-last"></div> </div> </div> </div>
Script Preparation code:
window.isParent = function(el) { return el.classList.contains('parent'); } window.findParent = function(el) { if (isParent(el)) return el if (!el.parentNode) return false return findParent(el.parentNode) } Benchmark.prototype.setup = function() { var element = document.getElementsByClassName('child-last')[0]; };
Tests:
Closest
element = element.closest('.parent')
Loop
while (element) { if (isParent(element)) break; if (!element.parentNode) break; element = element.parentNode; }
Recursion
element = findParent(element)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Closest
Loop
Recursion
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):
**Benchmark Overview** The provided benchmark compares three approaches to find the parent element of an HTML child element: `Closest`, `Loop`, and `Recursion`. The test case uses JavaScript and utilizes a custom library called `findParent` (explained later). **Options Comparison** 1. **Closest**: This approach uses the `closest()` method provided by modern browsers, which traverses up the DOM tree in search of an element that matches the specified selector. * Pros: Efficient, widely supported, and fast. * Cons: May not work for older browsers or specific edge cases. 2. **Loop**: This approach iterates through the DOM tree using a while loop to find the parent element. * Pros: Works in all browsers, including older ones, but may be slower due to the overhead of iteration. * Cons: More complex and error-prone than `Closest`. 3. **Recursion**: This approach calls the `findParent` function recursively until it finds a matching parent element. * Pros: Simple and easy to implement, works in all browsers. * Cons: May be slower due to recursion overhead, and can cause stack overflow errors for deep DOM trees. **Custom Library - findParent** The `findParent` library is used by the Recursion test case. It provides a simple function to find the parent element of an HTML element using iteration. The implementation is: ```javascript window.findParent = function(el) { if (isParent(el)) return el; if (!el.parentNode) return false; return findParent(el.parentNode); }; ``` The `isParent` function checks if the provided element is a parent of another element by checking its class list. **Special JS Feature - no special features are mentioned** **Other Alternatives** In addition to these three approaches, other alternatives could include: * Using the `parentNode` property directly: `element.parentNode` * Using a library like jQuery's `.parent()` method * Using a more complex approach involving regular expressions or DOM traversal APIs However, these alternatives may not be as efficient or widely supported as the `Closest`, `Loop`, and `Recursion` approaches.
Related benchmarks:
Recursion closest() vs loop closest()
Fastest way to list children: childNodes vs children vs firstChild/nextSibling vs firstElementChild/nextElementSibling v2 fixed
Finding parent element: Closest vs Loop vs Recursion
childNodes vs children vs firstChild/nextSibling vs firstElementChild/nextElementSibling (optimized html)
Comments
Confirm delete:
Do you really want to delete benchmark?