Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Lodash.get vs object-path vs Property dot notation (optional chaining)
(version: 0)
Modified from https://gist.github.com/acao/018fa0943da0e39c96f57fca64cd0c02
Comparing performance of:
Lodash get vs object path vs Native
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js" type="text/javascript"></script> <script src="https://cdn.rawgit.com/mariocasciaro/object-path/master/index.js"></script>
Script Preparation code:
var obj = { stuff: { things: { stuff: 'things', things: 'stuff', other: { stuff: 'things' } } } };
Tests:
Lodash get
_.get(obj, 'stuff.things.other.stuff');
object path
objectPath.get(obj, 'stuff.things.other.stuff');
Native
obj?.stuff?.things?.other?.stuff;
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
Lodash get
object path
Native
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/137.0.0.0 Safari/537.36
Browser/OS:
Chrome 137 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash get
14126017.0 Ops/sec
object path
5543987.0 Ops/sec
Native
214558032.0 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and explain what's being tested. The benchmark is designed to compare the performance of three ways to access nested properties in an object: Lodash's `get()` function, the `object-path` library, and native JavaScript's optional chaining (`?.`) syntax. **Lodash's `get()` function** Lodash is a popular utility library for JavaScript that provides various functions for tasks such as string manipulation, array manipulation, and more. The `get()` function is specifically designed to safely navigate nested objects, returning `undefined` if the path does not exist. In this benchmark, Lodash's `get()` function is used to access the nested property `stuff.things.other.stuff`. **object-path library** The object-path library provides a simple and expressive way to traverse and manipulate objects in JavaScript. The `get()` function in object-path works similarly to Lodash's `get()`, but with some differences in its behavior. In this benchmark, object-path is used to access the nested property `stuff.things.other.stuff`. **Native JavaScript's optional chaining (`?.`) syntax** The optional chaining operator (`?.`) was introduced in ECMAScript 2020 (ES2020) and provides a concise way to access nested properties in objects. The syntax `obj?.prop?.nestedProp` returns `undefined` if any of the levels are null or undefined. In this benchmark, native JavaScript's optional chaining is used to access the nested property `stuff.things.other.stuff`. **Pros and Cons** * Lodash's `get()` function: + Pros: Provides a safe way to navigate nested objects, handles errors elegantly. + Cons: Adds extra overhead due to the library itself. * object-path library: + Pros: Lightweight and easy to use, provides a simple way to traverse objects. + Cons: May not be as robust as Lodash's `get()` function for complex navigation scenarios. * Native JavaScript's optional chaining: + Pros: Concise syntax, lightweight, and built-in to modern browsers and Node.js environments. + Cons: May not work in older environments or with specific use cases. **Other considerations** When choosing between these approaches, consider the following: * Performance: If you need high-performance access to nested properties, native JavaScript's optional chaining might be a good choice. Lodash's `get()` function and object-path library may add unnecessary overhead. * Code readability: Optional chaining provides a concise syntax, but it can also make code harder to read if not used carefully. Lodash's `get()` function and object-path library provide more verbose syntax but are often easier to understand for complex navigation scenarios. **Alternatives** If you're looking for alternatives to these approaches, consider: * Using the `in` operator instead of optional chaining. * Utilizing the `hasOwnProperty()` method or `Object.prototype.hasOwnProperty()` to check if an object has a specific property before accessing it. * Implementing your own utility function for navigating nested objects. Keep in mind that these alternatives may not provide the same level of convenience and readability as Lodash's `get()` function, object-path library, or native JavaScript's optional chaining.
Related benchmarks:
Lodash.get vs Property dot notation
isEmpty vs Object.keys
Lodash.get vs Property dot notation - 2 deep
Lodash.get vs Property dot notation for complex data
Lodash.get vs Property dot notation with longer path
Comments
Confirm delete:
Do you really want to delete benchmark?