Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
get vs chain
(version: 0)
Comparing performance of:
Native vs Lodash.js filter
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.4/lodash.min.js"></script>
Script Preparation code:
var session = { corp: { id: 1, modules: { cockpit: true }, gtIds: { departement: 1, location: 2 }, constants: { anonymous_threshold: { em: 3, kv: 3, poll: 3 } } }, employee: { id: 1, super_admin: false, is_rh: false, isManager: false, isLocAdmin: false, isDepAdmin: false, links: { member: { groupTrees: { 1: {}, 2: {} } } }, adminOf: { department: { gTreeId: 1, groupIds: [] }, location: { gTreeId: 2, groupIds: [] } } } };
Tests:
Native
var anonymousThreshold = session.corp.constants.anonymous_threshold?.em || 3;
Lodash.js filter
var anonymousThreshold = _.get(session, 'corp.constants.anonymous_threshold.em', 3);
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Native
Lodash.js filter
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, compared, and considered. **Benchmark Overview** The benchmark is designed to measure the performance of two approaches: accessing nested properties directly (Native) versus using Lodash.js to access them (Lodash.js filter). **Options Compared** 1. **Native**: Directly accesses nested properties using dot notation (`session.corp.constants.anonymous_threshold?.em || 3`). 2. **Lodash.js filter**: Uses the `_.get()` method from Lodash.js library to access nested properties (`_.get(session, 'corp.constants.anonymous_threshold.em', 3)`). **Pros and Cons** 1. **Native**: * Pros: Simpler code, potentially faster since it avoids function call overhead. * Cons: May be less readable due to the need for multiple levels of dot notation, and may lead to errors if the property path is incorrect. 2. **Lodash.js filter**: * Pros: More readable code, provides a clear and explicit way to access nested properties, and handles errors by returning a default value (in this case, 3). * Cons: Slower due to the function call overhead, and requires an additional library dependency. **Library Used** Lodash.js is a popular JavaScript utility library that provides a set of functions for working with data structures. In this benchmark, `_.get()` is used to access nested properties in a more readable and explicit way. **Special JS Feature/Syntax** None mentioned. **Other Considerations** * The use of optional chaining (`?.`) in the Native approach allows for null or undefined checks, but may be less common in production code. * The Lodash.js filter approach uses a default value (3) to handle errors, which can be beneficial in certain scenarios. **Alternative Approaches** 1. **Arrow functions**: Could potentially provide a similar performance profile to Native, but with the added benefit of being more concise and readable. 2. **Destructuring assignment**: Might offer an alternative way to access nested properties, such as `const { anonymousThreshold } = session.corp.constants;`. 3. **Other library solutions**: Depending on the specific requirements, other libraries like Moment.js or Underscore.js might be suitable alternatives for accessing nested properties. In summary, the benchmark highlights the trade-offs between direct property access and using a utility library to navigate complex data structures. While Native offers simplicity and potentially faster execution, Lodash.js filter provides readability and error handling, making it an attractive option in certain scenarios.
Related benchmarks:
Lodash.get vs &&
Lodash.isEqual vs JSON.stringify Equality Comparison for array of objects with nested properties and many records
Optional Chaining versus _.get lodash without badObj
Lodash.isEqual vs JSON.stringify Equality Comparison for array of objects with nested properties and lots of records 222weqeqrq
Lodash.isEqual vs JSON.stringify and recusive Equality Comparison for array of objects with nested properties and lots of records
Comments
Confirm delete:
Do you really want to delete benchmark?