<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
const myObject = {
level1Key1: {
level2Key1: {
level3Key1: "value1",
level3Key2: "value2",
level3Key3: "value3",
level3Key4: "value4",
level3Key5: "value5"
},
level2Key2: {
level3Key6: "value6",
level3Key7: "value7",
level3Key8: "value8",
level3Key9: "value9",
level3Key10: "value10"
},
level2Key3: {
level3Key11: "value11",
level3Key12: "value12",
level3Key13: "value13",
level3Key14: "value14",
level3Key15: "value15"
}
},
level1Key2: {
level2Key4: {
level3Key1: "value1",
level3Key2: "value2",
level3Key3: "value3",
level3Key4: "value4",
level3Key5: "value5"
},
level2Key5: {
level3Key6: "value6",
level3Key7: "value7",
level3Key8: "value8",
level3Key9: "value9",
level3Key10: "value10"
},
level2Key6: {
level3Key11: "value11",
level3Key12: "value12",
level3Key13: "value13",
level3Key14: "value14",
level3Key15: "value15"
}
},
level1Key3: {
level2Key7: {
level3Key1: "value1",
level3Key2: "value2",
level3Key3: "value3",
level3Key4: "value4",
level3Key5: "value5"
},
level2Key8: {
level3Key6: "value6",
level3Key7: "value7",
level3Key8: "value8",
level3Key9: "value9",
level3Key10: "value10"
},
level2Key9: {
level3Key11: "value11",
level3Key12: "value12",
level3Key13: "value13",
level3Key14: "value14",
level3Key15: "value15"
}
}
};
myCopy = _.cloneDeep(myObject);
myCopy = JSON.parse(JSON.stringify(myObject));
myCopy = structuredClone(myObject);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash CloneDeep | |
Json Clone | |
structured Clone |
Test name | Executions per second |
---|---|
Lodash CloneDeep | 267002.0 Ops/sec |
Json Clone | 345872.2 Ops/sec |
structured Clone | 234659.6 Ops/sec |
In this benchmark, the performance of three different methods for creating a deep copy of a complex JavaScript object is compared. The three approaches being tested are:
Lodash's cloneDeep
:
myCopy = _.cloneDeep(myObject);
cloneDeep
function specifically is used to create a deep copy of a value, which means that it recursively clones every object, including nested objects.JSON Serialization:
myCopy = JSON.parse(JSON.stringify(myObject));
Structured Clone:
myCopy = structuredClone(myObject);
structuredClone
method is a newer addition to JavaScript that allows for cloning values that can include more complex types.The benchmark results indicate the number of executions per second for each cloning method on a specified browser and operating system environment:
cloneDeep
method, while slower, provides robustness in handling complex objects and unexpected data types.structuredClone
function, while native and very versatile, came in the lowest for this specific test case, indicating that while it is useful, its performance may not always match that of the other methods.Other potential alternatives for deep cloning include:
rfdc
(Really Fast Deep Clone) are designed specifically for high-performance deep cloning.Overall, the choice of method can depend on the complexity of the data being cloned, the need for performance, and the specific requirements of the application.