Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
set by path recursive vs while loop v1
(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 (!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 dive into the world of JavaScript microbenchmarks and explore what's being tested on this particular benchmark. **Benchmark Definition:** The benchmark definition is provided in JSON format, which represents two different approaches to set a value at a nested path in an object using recursion. There are three functions: 1. `setByPathWhile`: This function uses a while loop to iterate through the keys and navigate the nested object. 2. `setByPath`: This function uses recursion to traverse the object. **Options being compared:** The two options being compared are: * `setByPathWhile` (using a while loop) * `setByPath` (using recursion) These two approaches have different trade-offs in terms of performance, readability, and maintainability. **Pros and Cons:** 1. **setByPathWhile (while loop)**: * Pros: + Typically faster due to the avoidance of function call overhead. + Can be more readable for some developers who prefer a explicit loop structure. * Cons: + Requires manual management of the loop index and termination condition. + May require additional checks to handle edge cases (e.g., null or undefined values). 2. **setByPath (recursion)**: * Pros: + Can be more concise and easier to read, especially for deeper nesting levels. + Automatic handling of edge cases through function call overhead. * Cons: + May be slower due to the repeated function calls. + Can lead to stack overflow errors if not properly optimized. **Library and its purpose:** The `setByPath` function uses a technique called "memoization" to cache intermediate results. This is done using the `{}.hasOwnProperty.call(source, key)` expression, which checks if the source object has a property with the given key. If it does, the cached value is returned; otherwise, a new array or object is created. **Special JS feature/syntax:** There are no special JavaScript features or syntax mentioned in this benchmark definition. **Other alternatives:** There are other approaches to set values at nested paths in objects, such as: * Using a library like Lodash's `setIn` function * Utilizing the `in` operator with a closure * Implementing a hybrid approach that combines elements of both recursive and iterative methods These alternative approaches may offer different trade-offs in terms of performance, readability, and maintainability, but they can provide additional options for developers to consider when working with nested objects. In summary, this benchmark compares two approaches to set values at nested paths in objects: `setByPathWhile` (using a while loop) and `setByPath` (using recursion). The choice between these approaches depends on the specific requirements of your use case, including performance, readability, and maintainability.
Related benchmarks:
set by path recursive vs while loop v2
safeGet comparison (PR)
safeGet comparison (for PR)
safeGet comparison (for improvement PR)
Comments
Confirm delete:
Do you really want to delete benchmark?