<script src="https://cdn.jsdelivr.net/npm/immer@3.1.3/dist/immer.umd.min.js"></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
state = {
data: {
data1: {
data2: 'test'
}
}
};
const result = _.get(state, ['data', 'data1', 'data2'])
const result = state?.data?.data1?.data2
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
lodash get | |
CloneDeep |
Test name | Executions per second |
---|---|
lodash get | 2344650.0 Ops/sec |
CloneDeep | 5499033.5 Ops/sec |
Let's dive into the world of MeasureThat.net and explore what's tested in this benchmark.
Overview
MeasureThat.net is a platform that allows users to create and run JavaScript microbenchmarks, which are small tests designed to measure the performance of specific code snippets. The benchmark provided consists of two test cases: "lodash get" and "CloneDeep".
Benchmark Definitions
The benchmark definitions describe the code snippets being tested. Let's break them down:
_.get()
method is used to safely access nested properties of an object. In this case, the code snippet attempts to access the value of data2
within the data1
property of the state
object.const result = _.get(state, ['data', 'data1', 'data2']);
The Lodash library provides a convenient way to access nested properties without worrying about null or undefined values.
state
object using the cloneDeep()
method.const result = state?.data?.data1?.data2;
In modern JavaScript, we can use optional chaining (?.
) to access nested properties in a safe manner.
Options Compared
The benchmark compares two approaches:
_.get()
method to access the nested property.cloneDeep()
method.Both approaches have their pros and cons:
In general, the CloneDeep approach is a good choice when working with native JavaScript and prefer not to rely on external libraries. However, Lodash get provides a convenient way to access nested properties without worrying about null or undefined values, making it a popular choice among developers.
Other Considerations
When choosing between these approaches, consider the following factors:
cloneDeep()
method is generally faster than using Lodash for simple property accesses.Other Alternatives
If you don't like using Lodash or Immer, there are alternative approaches:
const result = state.data.data1.data2;
This approach requires more manual effort and may lead to null or undefined value checks.
Object.prototype.hasOwnProperty.call()
: This method provides a safer way to check if an object has a certain property before accessing it.const result = (state.data && state.data.data1 && state.data.data1.data2);
This approach requires careful handling of nested properties and may not be as convenient as using Lodash or CloneDeep.
Overall, the choice between these approaches depends on your specific use case, performance requirements, and personal preference. MeasureThat.net's benchmark provides a useful insight into how these approaches compare in terms of execution speed and performance.