<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: {
data_1: {
data_1_1: 'data_1_1',
data_1_2: 'data_1_2'
},
data_2: {
data_2_1: 'data_2_1',
data_2_2: {
data_2_2_1: 'value'
}
},
}
};
const result = immer.produce(state, draft => { draft.data.data_1.data_1_1 = 'data_1_1_updated', draft.data.data_2.data_2_2.data_2_2_1 = 'data_2_2_1' })
const result = _.cloneDeep(state);
result.data.data_1.data_1_1 = 'data_1_1_updated';
result.data.data_2.data_2_2.data_2_2_1 = 'data_2_2_1';
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Immer produce | |
Lodash clonedeep |
Test name | Executions per second |
---|---|
Immer produce | 109003.0 Ops/sec |
Lodash clonedeep | 258377.1 Ops/sec |
Benchmark Explanation
The provided JSON represents a JavaScript microbenchmark test on MeasureThat.net, comparing the performance of two libraries: Immer and Lodash.
What is tested?
The benchmark tests the execution time of two approaches:
produce
method: This method creates a new draft object from an existing state object, allowing for immutable updates to be made on the original state without affecting its original values.cloneDeep
function: This function creates a deep copy of an object, which can be used to create a new, independent copy of an existing object.Options compared
The benchmark compares the execution time of these two approaches:
produce
methodcloneDeep
functionPros and Cons
Immer's produce
method:
Pros:
Cons:
Lodash's cloneDeep
function:
Pros:
Cons:
Library purpose
Both Immer and Lodash are popular JavaScript libraries used for various purposes:
Special JS feature or syntax
The benchmark uses the const
keyword with ES6-style destructuring (e.g., draft.data.data_1.data_1_1 = 'data_1_1_updated';
) to create a new variable (draft
) from an existing state object. This is a common pattern in JavaScript, especially when working with functional programming concepts.
Other alternatives
For the purpose of this benchmark, the two libraries (Immer and Lodash) are the primary options being compared. However, other cloning methods or approaches might be used in different scenarios:
Object.assign()
method can be used for simple copying, but it has limitations when dealing with complex data structures.Please note that the choice of library or approach depends on the specific requirements of your project, such as performance, readability, and maintainability.