Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set by path recursive vs while loop v2
(version: 0)
Comparing performance of:
set by path recursive vs set by path while loop
Created:
3 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var user = { id: 1, email: 'bob@somewhere.com', personalInfo: { name: 'Robert', address: { road: 'Quartier Djinageryber', city: 'Timbuktu', country: 'Mali' } } } function setByPathWhile (keys, value, source) { let [currKey, nextKey] = keys let node = source let i = 1 while (nextKey !== undefined) { if (!{}.hasOwnProperty.call(node, currKey)) { node[currKey] = isNaN(nextKey) ? {} : [] } node = node[currKey] currKey = keys[i] nextKey = keys[++i] } node[currKey] = value return source } function setByPath ([key, nextKey, ...restOfKeys], value, source) { source[key] = (nextKey !== undefined) ? setByPath( [nextKey, ...restOfKeys], value, ({}.hasOwnProperty.call(source, key)) ? source[key] : isNaN(nextKey) ? {} : [] ) : value return source }
Tests:
set by path recursive
setByPath(['personalInfo', 'hobbies', 0], 'jogging', user)
set by path while loop
setByPathWhile(['personalInfo', 'hobbies', 0], 'jogging', user)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
set by path recursive
set by path while 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 provided benchmark and explain what's being tested. **Benchmark Definition** The benchmark is testing two different approaches to set values in an object using the `setByPath` function. 1. **Recursive Approach (`setByPathWhile`)**: This approach uses a while loop to navigate through the object properties recursively. 2. **Iterative Approach (`setByPath`)**: This approach uses an array of keys and iterates over them to set values in the object. **Options being compared** The two options being compared are: 1. `setByPathWhile` 2. `setByPath` **Pros and Cons of each approach:** 1. **Recursive Approach (`setByPathWhile`)**: * Pros: + Can handle complex path expressions with nested objects. + Might be more readable for developers familiar with recursive functions. * Cons: + May have performance overhead due to the use of a while loop and function calls. + Could lead to stack overflow errors for very deep path expressions. 2. **Iterative Approach (`setByPath`)**: * Pros: + Generally more efficient than recursive approaches, as it avoids function call overhead. + Can handle large path expressions without risk of stack overflow. * Cons: + Might be less readable for developers unfamiliar with array manipulation and key iteration. **Library used in the test case** The `setByPath` function is a custom implementation, but its purpose can be inferred from its usage. It appears to provide a convenient way to set values in an object using a path-like syntax. The library provides a way to recursively navigate through objects and set values at specific locations. **Special JS feature or syntax used** There are no special JavaScript features or syntaxes explicitly mentioned, but the use of `isNaN` to create empty arrays or objects is a common pattern in JavaScript. It's likely that this is just a convention used by the author of the `setByPath` function. **Other alternatives** For similar benchmarks, you might see other approaches such as: 1. Using a library like Lodash or Ramda for functional programming. 2. Implementing a custom recursive or iterative approach using JavaScript's built-in data structures (e.g., arrays, objects). 3. Utilizing web worker threads to parallelize the execution of multiple iterations. Keep in mind that these alternatives might not be directly comparable to the `setByPath` implementation, as they may have different performance characteristics or requirements for readability and maintainability.
Related benchmarks:
set by path recursive vs while loop v1
safeGet comparison (PR)
safeGet comparison (for PR)
safeGet comparison (for improvement PR)
Comments
Confirm delete:
Do you really want to delete benchmark?