Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Get props
(version: 0)
Get nested props from object
Comparing performance of:
get vs switch/case vs reduce
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { a: { b: { c: 'CCC' } } }; function get(obj, path) { var parts = path.split('.'); var property = Object.assign({}, obj); for (var i = 0; i < parts.length; i++ ) { property = property[parts[i]]; if (!property) { property = undefined; break; } } return property; } function switchCase(obj) { var res; Object.keys(obj).forEach((key1) => { switch(key1) { case 'a': Object.keys(obj[key1]).forEach((key2) => { switch(key2) { case 'b': Object.keys(obj[key1][key2]).forEach((key3) => { switch(key3) { case 'c': res = obj[key1][key2][key3]; break; } }); break; } }); break; } }); return res; } function reduce(obj, path) { var parts = path.split('.'); var property = Object.assign({}, obj); return parts.reduce((xs, x) => (xs && xs[x]) ? xs[x] : null, property) }
Tests:
get
get(obj, 'a.b.c')
switch/case
switchCase(obj)
reduce
reduce(obj, 'a.b.c')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
get
switch/case
reduce
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):
I'll break down the provided JSON and explain what's being tested, compared, and some considerations. **Benchmark Definition JSON** The benchmark definition defines three functions: 1. `get(obj, path)`: Retrieves nested properties from an object using dot notation (`path`). 2. `switchCase(obj)`: Iterates through a nested object structure using `switch` statements to retrieve values. 3. `reduce(obj, path)`: Similar to `get`, but uses the `reduce()` method to traverse the object. The benchmark also includes a script preparation code and HTML preparation code (which is empty in this case). **Individual Test Cases** There are three test cases: 1. `get(obj, 'a.b.c')`: Tests the `get()` function with a specific path (`'a.b.c'`) on an object with nested properties. 2. `switchCase(obj)`: Tests the `switchCase()` function without providing any specific input or path. 3. `reduce(obj, 'a.b.c')`: Tests the `reduce()` function with a specific path (`'a.b.c'`) on an object. **Comparison of Approaches** The three approaches differ in their execution and traversal strategies: 1. **`get()`**: Uses dot notation to traverse the object, which can be efficient for simple nested structures. However, it may not work well for deeply nested or complex objects. 2. **`switchCase()`**: Uses a nested `switch` statement to iterate through the object structure, which can lead to slower performance and more code complexity. This approach might be better suited for small, fixed-size data structures. 3. **`reduce()`**: Utilizes the `reduce()` method, which is optimized for traversing arrays and objects in a functional programming style. It's likely to be faster and more memory-efficient than the other two approaches. **Pros and Cons** Here are some pros and cons of each approach: 1. `get()` * Pros: Efficient for simple nested structures, easy to implement. * Cons: May not work well for deeply nested or complex objects. 2. `switchCase()` * Pros: Suitable for small, fixed-size data structures, can be more readable. * Cons: Slower performance, more code complexity. 3. `reduce()` * Pros: Optimized for traversing arrays and objects, likely faster and more memory-efficient. * Cons: May require more code knowledge and setup. **Other Considerations** When evaluating these approaches, consider the following factors: 1. **Data structure**: The size, shape, and complexity of the data being processed will influence the choice of approach. 2. **Performance**: Measured executions per second (EPs/s) can give an indication of which approach is faster. 3. **Code readability**: Simplicity and maintainability of the code are essential for larger projects or teams. **Alternative Approaches** For similar problems, you might consider these alternative approaches: 1. **Recursive functions**: Implementing recursive functions that traverse the object using dot notation or other traversal strategies. 2. **Iterative functions with arrays**: Utilizing iterative techniques to traverse objects using arrays and loops instead of recursion. 3. **Functional programming libraries**: Leveraging libraries like Lodash or Ramda, which provide optimized functions for traversing objects and arrays. Keep in mind that the choice of approach ultimately depends on the specific requirements, performance considerations, and personal preference.
Related benchmarks:
Get props
Get props
Map() vs Object
or vs some 2
Comments
Confirm delete:
Do you really want to delete benchmark?