Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash.get vs Lodash.property vs native vs native with optional chaining
(version: 0)
Comparing performance of:
Lodash get vs _property vs native vs native with optional chaining
Created:
2 years ago
by:
Guest
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', foo: { bar: "baz" } }; var p = _.property("foo.bar");
Tests:
Lodash get
_.get(person, 'foo.bar');
_property
p(person);
native
person.foo.bar;
native with optional chaining
person?.foo?.bar;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
Lodash get
_property
native
native with optional chaining
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
11 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/136.0.0.0 Safari/537.36 Edg/136.0.0.0
Browser/OS:
Chrome 136 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash get
11744163.0 Ops/sec
_property
11547978.0 Ops/sec
native
133247576.0 Ops/sec
native with optional chaining
130952544.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the provided benchmark and explain what's being tested. **Overview** The test creates an object `person` with nested properties, and then measures the performance of four different approaches to access those properties: 1. Lodash's `get()` function 2. Lodash's `_property()` function 3. Direct property access (native JavaScript) 4. Direct property access using optional chaining (`?.`) **Library: Lodash** Lodash is a popular JavaScript utility library that provides various helper functions for tasks like string manipulation, array manipulation, and more. In this benchmark, two Lodash functions are used: * `_.get()` : This function is designed to safely navigate nested object properties. * `_property()`: This function is similar to `_.get()`, but with some differences in how it handles certain edge cases. **Native JavaScript** The native JavaScript approach uses direct property access (`person.foo.bar`) to access the nested properties. This is a straightforward way to access properties, but it may be slower than using Lodash functions due to overhead and potential type checking. **Optional Chaining (?.)** Optional chaining is a new feature in JavaScript introduced in ECMAScript 2020. It allows you to access nested properties without throwing an error if the property doesn't exist. In this benchmark, optional chaining is used with direct property access (`person?.foo?.bar`). **Performance Comparison** The benchmark measures the performance of each approach by executing each test case multiple times (ExecutionsPerSecond) and reporting the average result. Here's a brief summary of the pros and cons of each approach: 1. **Lodash.get()**: Pros: provides safe navigation for nested properties, returns undefined if property doesn't exist, and is generally faster than direct access. * Cons: adds overhead due to Lodash function call. 2. **_property()**: Similar to `_.get()` but with some differences in handling edge cases. Pros: + May be faster than `_.get()` for certain use cases + Different behavior for certain edge cases * Cons: may not provide the same level of safety as `_.get()`, and has slightly different performance characteristics. 3. **Native JavaScript**: Pros: + Fastest approach due to minimal overhead - Simple to understand and implement * Cons: may throw an error if property doesn't exist, requires explicit type checking. **Optional Chaining (?.)** Pros: + Provides safe navigation with a single operator + Allows for shorter code * Cons: + Introduced in ECMAScript 2020, may not be supported by older browsers or environments + Can lead to confusion if not used carefully due to its different behavior compared to direct access. **Other Alternatives** For similar performance comparisons: 1. **Lodash.get()` vs `_.property()`: Compare the performance of these two functions with slightly different behavior. 2. **Native JavaScript** vs **Optional Chaining (?.)**: Compare the performance and trade-offs between direct property access and optional chaining. 3. **Using `in` operator**: Compare the performance of using the `in` operator to check if a property exists versus using optional chaining. In summary, this benchmark provides insights into the performance characteristics of different approaches to accessing nested properties in JavaScript, highlighting the trade-offs between safety, readability, and performance.
Related benchmarks:
Lodash.get vs Lodash.property vs native
Lodash.get vs Lodash.property vs native 2
Lodash.get vs Lodash.property vs native 3
deeply nested Lodash.get vs Lodash.property vs native
Comments
Confirm delete:
Do you really want to delete benchmark?