Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Recursive find reduce vs. for
(version: 0)
Comparing performance of:
Reduce vs For
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script>
Tests:
Reduce
const tree = [ { link: { url: '/de', text: 'link1' }, children: [ { link: { url: '/de/visible1', text: 'rendered on mobile and desktop' }, children: [] }, { link: { url: '/de/visible2', textSubNavigation: 'rendered on mobile only' }, children: [ { link: { url: '/de/2/3', textSubNavigation: 'test' }, children: [] } ] }, ] }, { link: { url: '/en', text: 'link2' }, children: [] }, { link: { url: '/hidden', textSubNavigation: 'never rendered (no text key)' }, children: [] }, ] const matcher = R.pathEq([ 'link', 'url' ], '/de/visible1') const findRecursivelyReduce = R.curry((items, matcherFn, childProp) => R.reduce((acc, item) => { if (acc) return acc if (matcherFn(item)) return item if (R.has(childProp, item)) return findRecursivelyReduce(R.prop(childProp, item), matcherFn, childProp) return false }, false, items)) findRecursivelyReduce(tree, matcher, 'children')
For
const tree = [ { link: { url: '/de', text: 'link1' }, children: [ { link: { url: '/de/visible1', text: 'rendered on mobile and desktop' }, children: [] }, { link: { url: '/de/visible2', textSubNavigation: 'rendered on mobile only' }, children: [ { link: { url: '/de/2/3', textSubNavigation: 'test' }, children: [] } ] }, ] }, { link: { url: '/en', text: 'link2' }, children: [] }, { link: { url: '/hidden', textSubNavigation: 'never rendered (no text key)' }, children: [] }, ] const matcher = R.pathEq([ 'link', 'url' ], '/de/visible1') const findRecursivelyFor = R.curry((items, matcherFn, childProp) => { for (let i = 0; i < items.length; i += 1) { const item = items[i] if (matcherFn(item)) return item if (R.has(childProp, item)) { const matchingSubItem = findRecursivelyFor(R.prop(childProp, item), matcherFn, childProp) if (matchingSubItem) return matchingSubItem } } }) findRecursivelyFor(tree, matcher, 'children')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Reduce
For
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 dive into the world of JavaScript microbenchmarks. **Benchmark Overview** The provided benchmark is designed to compare two approaches: recursive find-reduce and for-loops. The test case uses a recursive data structure, represented as an array of objects with nested properties. The goal is to traverse this structure and find a specific node that matches a given condition. **Recursive Find-Reduce Approach** This approach uses the Ramda library's `R.curry` function to create a curried version of the `findRecursivelyReduce` function. This function takes three arguments: 1. `items`: the array of objects to search through. 2. `matcherFn`: a function that takes an object and returns `true` if it matches the condition. 3. `childProp`: the property name that indicates the presence of child objects. The function uses a recursive reduce operation to traverse the tree-like structure. It starts with an initial value of `false` and iterates through each item in the array. If the item matches the condition, it returns the item. If it has child objects, it recursively calls itself on those child objects until it finds a match. **For-Loop Approach** This approach uses a traditional for-loop to iterate through each item in the array. It takes three arguments: 1. `items`: the array of objects to search through. 2. `matcherFn`: a function that takes an object and returns `true` if it matches the condition. 3. `childProp`: the property name that indicates the presence of child objects. The loop iterates through each item in the array, checking if it matches the condition using the `matcherFn`. If it does, it returns the item. If it has child objects, it recursively calls itself on those child objects until it finds a match. **Pros and Cons** Recursive find-reduce approach: Pros: * Often more concise and elegant than traditional for-loops. * Can be faster in some cases due to the optimized reduce operation. Cons: * May have higher overhead due to the recursive function calls. * Can lead to stack overflow errors if the tree is too deep. For-loop approach: Pros: * Often more straightforward and easy to understand for beginners. * Can avoid potential stack overflow errors by not using recursive function calls. Cons: * May be slower than the recursive approach due to the overhead of the loop. * Can be less concise and more verbose than the recursive approach. **Library: Ramda** Ramda is a popular JavaScript library that provides a set of functional programming utilities. The `R.curry` function creates a curried version of a function, which allows for more expressive and flexible code. In this case, it's used to create a curried version of the `findRecursivelyReduce` function. **Other Alternatives** If you're interested in exploring alternative approaches, here are a few options: * Using a traditional loop with `Array.prototype.forEach` * Using a recursive function without Ramda's `R.curry` function * Using a different library or framework that provides optimized traversal algorithms (e.g., React's `useMemo` hook) * Using a more specialized data structure, such as a trie or a suffix tree, to improve performance. Ultimately, the choice of approach depends on your specific use case and performance requirements.
Related benchmarks:
ramdajs contains
ramdajs contains
lodash fp vs lodash vs vanilla vs ramda
Find (Native vs Ramda)
Ramda vs. Lodash vs Natice (filter, find)
Comments
Confirm delete:
Do you really want to delete benchmark?