<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var fullObject = {
a: {
b: {
c: 'value'
}
}
}
var emptyObject = {}
var { a } = fullObject || {}
var { b } = a || {}
var { c } = b || {}
var c = _.get(fullObject, 'a.b.c')
var { a } = emptyObject || {}
var { b } = a || {}
var { c } = b || {}
var c = _.get(emptyObject, 'a.b.c')
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Destructuring assignment on full object | |
Lodash get() on full object | |
Destructuring assignment on empty object | |
Lodash get() on empty object |
Test name | Executions per second |
---|---|
Destructuring assignment on full object | 151678336.0 Ops/sec |
Lodash get() on full object | 13643513.0 Ops/sec |
Destructuring assignment on empty object | 160986896.0 Ops/sec |
Lodash get() on empty object | 16430197.0 Ops/sec |
Let's dive into the world of JavaScript microbenchmarks.
Benchmark Overview
The provided benchmark compares two approaches for accessing nested object properties: destructuring assignment and using the lodash.get()
function.
Destructuring Assignment
Destructuring assignment is a syntax feature introduced in ECMAScript 2015 (ES6). It allows you to extract values from objects into separate variables. In this benchmark, we have three test cases:
fullObject
).emptyObject
).The Benchmark Definition
code for these tests is:
var { a } = fullObject || {};
var { b } = a || {};
var { c } = b || {};
// or
var { a } = emptyObject || {};
var { b } = a || {};
var { c } = b || {};
Lodash.get() Function
lodash.get()
is a utility function from the Lodash library, which allows you to safely navigate nested objects and retrieve values. In this benchmark, we have two test cases:
fullObject
).emptyObject
).The Benchmark Definition
code for these tests is:
var c = _.get(fullObject, 'a.b.c');
// or
var c = _.get(emptyObject, 'a.b.c');
Comparison of Approaches
Both approaches have their pros and cons:
Destructuring Assignment:
Pros:
Cons:
Lodash.get() Function:
Pros:
Cons:
Library (Lodash)
lodash.get()
is a utility function from the Lodash library, which provides a set of functional programming helpers for JavaScript. It's designed to make working with data structures like arrays, objects, and maps easier and more efficient. The get()
function is specifically designed to safely navigate nested objects and retrieve values.
Special JS Feature (ES6 Destructuring Assignment)
As mentioned earlier, destructuring assignment requires ECMAScript 2015 (ES6) or later syntax support. This feature has been widely adopted in modern JavaScript development, but older browsers and versions may not support it.
Other Considerations
When choosing between these approaches, consider the following factors:
get()
function might be a better option due to its explicit and safe navigation mechanism.get()
function is a safer choice.Alternatives
If you're looking for alternative approaches or want to explore other options, consider the following:
get()
function, you can use object.hasOwn()
to check if an object property exists before accessing it.