<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>
<script src="https://cdn.jsdelivr.net/npm/uuid@8.3.2/dist/umd/uuidv4.min.js"></script>
const numSiblings = 500;
const depth = 3;
state = {
data: {
data1: {
data2: 'test',
siblings: Array.from({
length: numSiblings
}).map(() => ({
id: uuidv4()
})),
children: Array.from({
length: depth
}).map(() => ({
id: uuidv4(),
data: {
data1: {
data2: 'test',
siblings: Array.from({
length: numSiblings
}).map(() => ({
id: uuidv4()
})),
children: Array.from({
length: depth
}).map(() => ({
id: uuidv4(),
data: {
data1: {
data2: 'test',
siblings: Array.from({
length: numSiblings
}).map(() => ({
id: uuidv4()
})),
children: Array.from({
length: depth
}).map(() => ({
id: uuidv4(),
data: {
data1: {
data2: 'test',
siblings: Array.from({
length: numSiblings
}).map(() => ({
id: uuidv4()
})),
},
},
})),
},
},
})),
},
},
})),
},
data3: Array.from({
length: 1000
}).map(() => ({})),
},
};
const result = immer.produce(state, draft => draft)
const result = structuredClone(state);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
immer.produce() | |
structuredClone |
Test name | Executions per second |
---|---|
immer.produce() | 6504997.0 Ops/sec |
structuredClone | 240.5 Ops/sec |
The benchmark defined in the JSON compares two different approaches to creating deep copies of a complex JavaScript object: using the Immer library's produce
function and the built-in structuredClone feature.
Immer's produce
Function:
const result = immer.produce(state, draft => draft);
Structured Clone:
const result = structuredClone(state);
structuredClone
function provides a native way to create deep copies of JavaScript objects, including handling of various types like Date, Map, Set, and more.structuredClone
has been observed to be faster in certain scenarios compared to libraries due to optimizations in JavaScript engines.structuredClone
: A built-in JavaScript function to create a deep copy of objects. It is part of the ECMAScript specification, providing developers with a powerful way to handle complex data structures without needing external libraries.The benchmark results indicate that Immer's produce
function can handle around 2.27 million executions per second, whereas structuredClone achieves approximately 140 executions per second. This indicates that, at least in this specific case and context, Immer is significantly faster than structuredClone, which may seem counterintuitive given the typical expectations of built-in functions.
_.cloneDeep()
. It provides flexibility and various other utility functions but can have similar downsides to Immer in terms of additional dependency and performance overhead compared to native functions.JSON.stringify()
and JSON.parse()
): A commonly used method for cloning objects. However, it has limitations with non-serializable types like functions, undefined, or Infinity, making it less robust than structuredClone or the Immer approach.This benchmark provides insights into the performance and usability trade-offs between using an external library like Immer and utilizing native JavaScript features for deep cloning. The choice between these approaches will depend on specific project needs, such as the complexity of state management, performance requirements, and overall code maintainability.