Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Dexla - Lodash Get
(version: 0)
Comparing performance of:
Native vs Lodash Get
Created:
one year ago
by:
Registered User
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
Tests:
Native
const myObj = { test: { otherTest: undefined } } const result = myObj?.test?.otherTest?.secondTest ?? 'hello'
Lodash Get
const myObj = { test: { otherTest: undefined } } const result = _.get(myObj, 'test.otherTest.secondTest', 'hello')
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Native
Lodash Get
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
one year ago
)
User agent:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36
Browser/OS:
Chrome 124 on Mac OS X 10.15.7
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Native
16209003.0 Ops/sec
Lodash Get
5136470.5 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
**Overview** MeasureThat.net is a website that allows users to create and run JavaScript microbenchmarks. The provided JSON data represents two benchmark tests: "Native" and "Lodash Get". We'll break down what each test case measures, the options being compared, pros and cons of those approaches, and other considerations. **Benchmark Tests** 1. **Native**: This test case uses native JavaScript syntax to access a nested object property. ```javascript const myObj = { test: { otherTest: undefined } } const result = myObj?.test?.otherTest?.secondTest ?? 'hello' ``` The goal is to measure how fast this code executes. 2. **Lodash Get**: This test case uses the `_.get()` method from the Lodash library to access a nested object property. ```javascript const myObj = { test: { otherTest: undefined } } const result = _.get(myObj, 'test.otherTest.secondTest', 'hello') ``` The goal is to measure how fast this code executes. **Comparison of Options** In the "Native" test case: * The `?.` operator is used to safely navigate the nested object. * The nullish coalescing operator (`??`) is used to provide a default value if any part of the navigation is undefined or null. In the "Lodash Get" test case: * The Lodash library provides a convenient function for accessing nested object properties, avoiding the need for manual type checking and null checks. * This approach may be slower due to the overhead of function calls and potential caching issues. **Pros and Cons** Native Approach (`.?.` and `??`): Pros: * Faster execution since it avoids the overhead of a function call. * More concise code. Cons: * Requires manual type checking and null checks, which can lead to errors if not done correctly. * Limited support for complex navigation patterns. Lodash Get Approach (`_.get()`): Pros: * Easier to read and maintain, as it abstracts away the complexity of nested object access. * Works with more complex navigation patterns, but may be slower due to function call overhead. Cons: * Slower execution due to the overhead of a function call. * May lead to caching issues or unexpected behavior if not used carefully. **Library: Lodash** Lodash is a popular JavaScript utility library that provides various functions for tasks such as array manipulation, object access, and more. In this case, `_.get()` is used to access nested object properties in a convenient and readable way. **Special JS Feature/Syntax: Nullish Coalescing Operator (`??`)** The nullish coalescing operator was introduced in ECMAScript 2020 (ES12) as a new operator that allows you to provide a default value if an operand is either `null` or `undefined`. ```javascript const result = value ?? defaultValue; ``` This operator is not enabled by default and requires explicit enablement in the browser or runtime environment. **Alternatives** Other alternatives for accessing nested object properties include: 1. **Brackets (`[]`)**: This approach uses bracket notation to access an object property. ```javascript const result = myObj['test']['otherTest'] ``` 2. **Dot Notation (`.`)**: Similar to brackets, but with dot notation instead. ```javascript const result = myObj.test.otherTest ``` These approaches are more concise than the Lodash Get method, but may be slower due to the overhead of property access. 3. **Manual type checking and null checks**: This approach requires manual code to check for existence and types before accessing nested properties. Each of these alternatives has its pros and cons, and the choice ultimately depends on the specific use case and performance requirements.
Related benchmarks:
hasVshasOwnProperty
es6 destructuring vs lodash _.get asjfoasijdfio
Lodash vs rama vs pure
isEmpty vs. vanilla
Lodash isString fork
Comments
Confirm delete:
Do you really want to delete benchmark?