<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
}
const type = person.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 | 2929418.2 Ops/sec |
Lodash get array | 12417788.0 Ops/sec |
Native | 865745856.0 Ops/sec |
Native with checking for missing object | 484177152.0 Ops/sec |
?. | 401172160.0 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Overview
The benchmark compares the performance of four different approaches to access nested properties in JavaScript:
_.get
with string indexing_.get
with array indexingLodash .get
Lodash is a popular JavaScript utility library that provides various functions to simplify common programming tasks.
_.get
is a function that allows you to access nested properties in an object using either string or array indexing.person.data.type
).const type = _.get(person, ['data', 'type'])
).