Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash.get vs safeGet vs get vs optional chaining vs youDontNeedGet
(version: 1)
Comparing performance of:
Lodash get vs safeGet vs optional chaining vs get vs youDontNeedGet
Created:
3 years ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/lodash/4.16.0/lodash.min.js"></script>
Script Preparation code:
var person = {name: 'Frederick', lastName: 'Corcino Alejo', nested: {name: 'test'}}; function safeGet(obj, path, defaultValue) { return path.split('.').reduce((xs, x) => (xs && xs[x]) ? xs[x] : defaultValue, obj); } function get(obj, path, defValue) { // If path is not defined or it has false value if (!path) return undefined // Check if path is string or array. Regex : ensure that we do not have '.' and brackets. // Regex explained: https://regexr.com/58j0k const pathArray = Array.isArray(path) ? path : path.match(/([^[.\]])+/g) // Find value const result = pathArray.reduce( (prevObj, key) => prevObj && prevObj[key], obj ) // If found value is undefined return default value; otherwise return the value return result === undefined ? defValue : result } // Another "get" implementation // https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore#_get function youDontNeedGet(obj, path, defaultValue = undefined) { const travel = regexp => String.prototype.split .call(path, regexp) .filter(Boolean) .reduce((res, key) => (res !== null && res !== undefined ? res[key] : res), obj); const result = travel(/[,[\]]+?/) || travel(/[,[\].]+?/); return result === undefined || result === obj ? defaultValue : result; };
Tests:
Lodash get
_.get(person, 'name', ''); _.get(person, 'nested.name', '');
safeGet
safeGet(person, 'name', ''); safeGet(person, 'nested.name', '');
optional chaining
person?.name; person?.nested?.name;
get
get(person, 'name', null) get(person, 'nested.name', null)
youDontNeedGet
youDontNeedGet(person, 'name', null) youDontNeedGet(person, 'nested.name', null)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (5)
Previous results
Fork
Test case name
Result
Lodash get
safeGet
optional chaining
get
youDontNeedGet
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 explanation of what's being tested in this benchmark. **Overview** The benchmark measures the performance difference between four different approaches to access nested properties in an object: Lodash's `_.get`, `safeGet`, and two alternative implementations (`youDontNeedGet` and a simple `get` function). The test cases use a person object with nested properties to demonstrate these approaches. **Options compared** The benchmark compares the following options: 1. **Lodash `.get`**: Uses Lodash's `_.get` method, which is designed to access nested properties in an object. 2. **safeGet**: A custom implementation that uses a simple recursive approach to access nested properties. 3. **optional chaining**: The dot notation (`person?.name`) and optional chaining (`person?.nested?.name`) syntax, which are available in modern JavaScript engines (including Chrome 105). 4. **youDontNeedGet**: Another alternative implementation that uses a regular expression to split the property path into an array of keys. **Pros and Cons** Here's a brief summary of each approach: * **Lodash `.get`**: + Pros: Well-designed, efficient, and widely adopted. + Cons: Requires Lodash library inclusion. * **safeGet**: + Pros: Custom implementation can be optimized for performance. + Cons: Recursive approach may lead to stack overflow errors for deep nested properties. * **optional chaining**: + Pros: Simple, readable, and efficient. + Cons: Limited support in older JavaScript engines (pre-Chrome 105). * **youDontNeedGet**: + Pros: Creative use of regular expressions to split the property path. + Cons: Less readable than other approaches, and may lead to performance issues. **Other considerations** The benchmark measures the executions per second for each test case. This suggests that the goal is to determine which approach provides the best performance in terms of speed. **Library and syntax usage** * Lodash's `_.get` method uses a dot notation (`_.get(person, 'name', '')`) to access nested properties. * The `safeGet` implementation uses a simple recursive approach with a loop. * Optional chaining uses the dot notation (`person?.name`) followed by the optional chaining operator (`?.`). * The `youDontNeedGet` implementation uses a regular expression to split the property path into an array of keys. **Alternatives** If you're interested in exploring alternative approaches, here are some options: * Use the `in` operator to access nested properties, e.g., `person['nested']['name']`. * Use a library like Underscore.js (similar to Lodash) or a custom implementation with a loop. * Experiment with other optional chaining syntax variations, such as using bracket notation (`person['nested'].name`) instead of dot notation. Keep in mind that the best approach will depend on your specific use case and performance requirements.
Related benchmarks:
Lodash.get vs safeGet vs Native
Lodash.get vs safeGet vs Native vs Native?
lodash.get vs optional chaining vs safeGet
Lodash.get vs Lodash.property vs native test
Comments
Confirm delete:
Do you really want to delete benchmark?