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:
get1 vs get2 vs get3
Created:
8 years ago
by:
Guest
Jump to the latest result
Script Preparation code:
var obj = { a: { b: { c: 'CCC' } } }; function get1(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 get2(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 get3(obj, path) { var parts = path.split('.'); return parts.reduce((xs, x) => (xs && xs[x]) ? xs[x] : null, obj) }
Tests:
get1
get1(obj, 'a.b.c')
get2
get2(obj)
get3
get3(obj, 'a.b.c')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
get1
get2
get3
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 Explanation** The provided JSON represents a JavaScript microbenchmarking test suite, where the goal is to measure the performance of three different functions: `get1`, `get2`, and `get3`. Each function aims to retrieve nested properties from an object. **Function Overview** 1. **`get1(obj, path)`**: This function takes an object `obj` and a path string `path` as input. It splits the path into individual property names using `.` as the separator and then iterates through each part of the path to find the corresponding value in the object. If any intermediate property is missing, it sets the current property to `undefined`. 2. **`get2(obj)`**: This function iterates over the top-level properties of the input object `obj`. For each property, it recursively checks if the nested object has a specific key-value pair using nested loops and `switch` statements. 3. **`get3(obj, path)`**: This function takes an object `obj` and a path string `path` as input. It splits the path into individual property names using `.` as the separator and then uses the `reduce()` method to find the corresponding value in the object. If any intermediate property is missing, it returns `null`. **Comparison of Approaches** The three functions differ in their approach to handling nested properties: 1. **`get1`**: Iterative approach with explicit looping over property names. * Pros: + Easy to understand and maintain. + Can handle complex path structures. * Cons: + May be slower due to explicit looping. 2. **`get2`**: Recursive approach using nested loops and `switch` statements. * Pros: + More concise and expressive code. + Handles nested properties efficiently. * Cons: + Can be harder to understand and debug due to complex nesting. + May have performance overhead due to recursive function calls. 3. **`get3`**: Functional approach using `reduce()` method. * Pros: + More concise and expressive code. + Handles nested properties efficiently with a single pass. **Library Usage** None of the functions explicitly use any external libraries or frameworks, although they may rely on built-in JavaScript methods (e.g., `Object.assign()`, `Array.prototype.forEach()`). **Special JS Features/Syntax** The benchmark does not explicitly use any special JavaScript features or syntax, such as arrow functions, async/await, or modern ES modules. **Alternatives** If the benchmark were to be rewritten using alternative approaches, some options could include: * Using a library like Lodash or Underscore.js for functional programming and property access. * Employing a more declarative approach with data-driven testing frameworks like Jest or Mocha. * Utilizing modern JavaScript features like async/await or Promises for handling asynchronous operations. * Implementing the benchmark using a different programming language, such as Python or Ruby, if it were to be ported. Keep in mind that these alternatives would require significant changes to the benchmark code and may affect its accuracy or expressiveness.
Related benchmarks:
Test values
for-in vs object.keys v3
Object.keys vs Object.values vs Object.entries creation
for in vs for of --
For in vs Object.*.forEach vs Object.values 2
Comments
Confirm delete:
Do you really want to delete benchmark?