<script src="https://cdn.jsdelivr.net/lodash/4.16.0/lodash.min.js"></script>
var person = {data: {type: 'Frederick'}};
_.get(person, 'data.type');
const type = _.get(person, ['data', 'type']);
const type = person.data.type
const data = person.data
if(data !== null){
const type = data.type
}
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash get string | |
Lodash get array | |
Native | |
Native with checking for missing object |
Test name | Executions per second |
---|---|
Lodash get string | 2976911.2 Ops/sec |
Lodash get array | 4487002.0 Ops/sec |
Native | 17036738.0 Ops/sec |
Native with checking for missing object | 17072112.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks on MeasureThat.net.
Benchmark Overview
The benchmark compares the performance of three approaches to access the type
property within an object:
get()
function for accessing nested properties.type
property using dot notation (person.data.type
).type
property (const type = person.data !== null ? data.type : undefined;
).Comparison of Options
get()
function to access nested properties. This approach provides a convenient way to handle nested object structures, but may incur additional overhead due to the function call.type
property using dot notation. This approach is simple and efficient, as it only requires a single property access.type
property. This approach balances the simplicity of the native approach with the safety of checking for missing object properties.Library and Purpose
get()
function is designed to handle nested property access in an elegant and flexible way.Special JS Features
There are no special JavaScript features or syntaxes used in this benchmark. The focus is on comparing the performance of different approaches to access nested properties within an object.
Alternatives
If you're looking for alternative approaches to accessing nested properties in your code, consider the following:
In conclusion, this benchmark provides a useful comparison of three approaches to accessing nested properties within an object: Lodash's get()
function, native dot notation, and native with null checking. By understanding the pros and cons of each approach, you can make informed decisions about how to optimize your code for performance and readability.