Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash.get vs safeGet vs Native vs Native?
(version: 0)
Comparing performance of:
Lodash get vs safeGet vs Native vs Native?
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.21/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); }
Tests:
Lodash get
_.get(person, 'name', ''); _.get(person, 'nested.name', '');
safeGet
safeGet(person, 'name', ''); safeGet(person, 'nested.name', '');
Native
person.name; person.nested.name;
Native?
person?.name; person?.nested?.name;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Lodash get
safeGet
Native
Native?
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 definition and test cases. **What is being tested?** The benchmark measures the performance of three different approaches to access nested properties in an object: 1. `_.get(person, 'name', '');` from Lodash: A utility function that retrieves a value from an object using a dot notation path. 2. `safeGet(person, 'name', '');` from a custom script: A simple recursive function that splits the property path into individual keys and accesses them on the object. 3. `person.name; person.nested.name;` (without parentheses): Direct access to nested properties without any explicit path or function call. 4. `person?.name; person?.nested?.name;` (with optional chaining): Accessing nested properties using the optional chaining operator (`??`) to avoid null pointer exceptions. **Options compared** The benchmark compares the performance of: * Lodash's `_.get()` with and without a default value * The custom `safeGet()` function * Direct access to nested properties without any explicit path or function call * Optional chaining using the `?.` operator **Pros and cons of each approach:** 1. **Lodash's `_.get()`**: Pros: * More concise and readable syntax * Handles null pointer exceptions by providing a default value * Well-tested and widely adopted library Cons: * Adds an additional dependency (Lodash) to the codebase 2. **Custom `safeGet()` function**: Pros: * No external dependencies or library overhead * Customizable path splitting logic Cons: * More verbose and error-prone syntax 3. **Direct access to nested properties**: Pros: * Simple and straightforward syntax Cons: * Unpredictable behavior if the property does not exist 4. **Optional chaining (`?.` operator)**: Pros: * More concise and readable syntax * Reduces risk of null pointer exceptions Cons: * Requires modern JavaScript versions (ECMAScript 2020+) to be supported **Library usage** The `safeGet()` function uses a simple recursive approach to split the property path into individual keys and access them on the object. This implementation is straightforward but may not be optimized for performance. **Special JS features or syntax** Optional chaining (`?.` operator) is used in the benchmark, which is a relatively new feature introduced in ECMAScript 2020+. While it provides a more concise way to access nested properties, it requires modern JavaScript versions to be supported. In older browsers and environments, this approach may not work as expected. **Alternatives** If you prefer not to use Lodash or the custom `safeGet()` function, you can consider using other libraries or implementations that provide similar functionality, such as: * Other utility libraries like Underscore.js or Ramda * Custom implementations using more advanced path splitting techniques (e.g., using a regex-based approach) * Modern JavaScript alternatives to optional chaining, such as the `?.` operator's precursor in older browsers (e.g., Mozilla's "optional chaining" polyfill) Keep in mind that the choice of implementation depends on your specific use case, performance requirements, and personal preferences.
Related benchmarks:
Comparing performance of: Lodash get vs Native with object checking
lodash.get vs native JS continuous checks
Lodash.get vs Property dot notation @movlan
Lodash has vs Native Javascript
Lodash.get vs Property dot notation with longer path
Comments
Confirm delete:
Do you really want to delete benchmark?