Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash.get vs native with optional chaining
(version: 0)
Comparing performance of:
Lodash get vs Native
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: { firstName:'Frederick', lastName: 'Corcino Alejo' } };
Tests:
Lodash get
_.get(person, 'name.firstName'); _.get(person, 'name.nothing');
Native
person.name.firstName person.name?.nothing
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash get
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.1:latest
, generated one year ago):
Let's dive into the details of this benchmark. **What is being tested?** The test case compares two approaches for accessing nested properties in JavaScript objects: 1. Using Lodash's `_.get` function 2. Using native JavaScript with optional chaining (`?.`) **What options are compared?** Two test cases are run, each comparing the performance of the two approaches: 1. **Lodash get**: This test uses Lodash's `_.get` function to access nested properties. * The script preparation code loads Lodash version 4.16.0 from a CDN. * The benchmark definition contains two calls to `_.get`: one that accesses an existing property (`name.firstName`) and another that attempts to access a non-existent property (`name.nothing`). 2. **Native**: This test uses native JavaScript with optional chaining (`?.`) to access nested properties. * No additional library is loaded for this test case. * The benchmark definition contains two expressions: one that accesses an existing property (`person.name.firstName`) and another that attempts to access a non-existent property (`person.name.nothing`). **Pros/Cons of each approach** Here's a brief summary: 1. **Lodash get** * Pros: + Provides a concise way to access nested properties. + Handles null or undefined values elegantly (returns `undefined` if the path is not found). * Cons: + Requires loading an additional library (Lodash), which may impact performance and add dependencies. + May be overkill for simple use cases. 2. **Native with optional chaining (`?.`)** * Pros: + No external libraries are required, making it a lightweight solution. + Can be used in conjunction with other JavaScript features (e.g., destructuring, `for...in` loops). * Cons: + May lead to slower performance compared to Lodash's optimized implementation (as seen in the benchmark results). + Does not handle null or undefined values elegantly; instead, it returns `undefined`. **Other considerations** When choosing between these two approaches, consider the following factors: 1. **Performance**: If you need maximum performance and are willing to trade off some convenience, native JavaScript with optional chaining might be a better choice. 2. **Code readability**: If code readability is essential, Lodash's `_.get` function can provide a more concise way to access nested properties. 3. **Library dependencies**: If you're working on a project that already uses Lodash, using the `_.get` function might be a good fit. **What library is used?** Lodash is a popular JavaScript utility library that provides a wide range of functions for tasks like array and object manipulation, functional programming, and more. In this test case, it's used specifically for its `_.get` function to access nested properties in an object. **Special JS feature or syntax?** Yes, the native approach uses JavaScript's optional chaining (`?.`) feature, which is a shorthand way to access nested properties while avoiding errors if any part of the path is null or undefined. This feature was introduced in ECMAScript 2020 (ES2020) and has been supported by most modern browsers since then.
Related benchmarks:
Lodash vs native
Comparing performance of: Lodash get vs Native with object checking
Lodash.get vs Property dot notation @movlan
Lodash.get vs Property dot notation with longer path
Lodash.get vs Optional chaining 1234
Comments
Confirm delete:
Do you really want to delete benchmark?