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
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 (let 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; }
Tests:
get1
get1(obj, 'a.b.c')
get2
get2(obj)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
get1
get2
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 JSON and explain what's being tested. **Benchmark Definition JSON** The benchmark is designed to test two JavaScript functions: `get1` and `get2`. These functions are responsible for retrieving nested properties from an object. **Function get1** * Purpose: The `get1` function takes two parameters: an object (`obj`) and a path (e.g., `'a.b.c'`). It splits the path into individual parts and iterates through them to retrieve the corresponding property value. * Approach: This approach uses recursion-like behavior, where it updates the `property` variable in each iteration to point to the next level of nested properties. **Function get2** * Purpose: The `get2` function also retrieves nested properties from an object but uses a more brute-force approach. It iterates through each key in the object and checks if the current key matches any part of the path. * Approach: This approach is less recursive and more iterative, using a switch-case statement to navigate through the nested properties. **Pros and Cons** * **get1** * Pros: * More concise code * Easier to read and understand for some developers * Cons: * May be less efficient due to repeated property assignments * May lead to stack overflow errors if the object is extremely deeply nested * **get2** * Pros: * More efficient since it avoids repeated property assignments * Can handle very deeply nested objects without risk of stack overflows * Cons: * Code can be more verbose and harder to read, especially for complex paths **Other Considerations** * **Object Creation**: The benchmark creates a simple object with nested properties (`obj = { a: { b: { c: 'CCC' } } };`) and passes it to the `get1` and `get2` functions. * **Device Platform and Browser**: The test runs on Windows devices with Chrome 63 browser, which is specified in the benchmark definition. **Library Usage** There are no explicit libraries used in these functions. However, `Object.assign()` is a built-in JavaScript method that can be considered a lightweight library. **Special JS Features or Syntax** No special features or syntax are mentioned in this example, but the use of `switch` statements and array methods like `forEach()` indicate modern JavaScript usage. Now let's move on to some alternatives for similar benchmarks: 1. **Benchmarking Frameworks**: Tools like Benchmark.js, JSDoc's benchmark module, or even Python libraries like `timeit` can be used to create and run microbenchmarks. 2. **Other Microbenchmarking Libraries**: `microbenchmark`, `benchmark`, and others provide more extensive features for creating custom benchmarks. 3. **Alternative Scripting Languages**: If the focus shifts from JavaScript to another language (like C, Java, or Python), it would be necessary to re-write the benchmark using those languages. These alternatives offer a wide range of tools and libraries to choose from, depending on the specific needs and requirements of the project.
Related benchmarks:
Get props
Get props
Map() vs Object
or vs some 2
Comments
Confirm delete:
Do you really want to delete benchmark?