<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
<script type="module">
import fastCopy from 'https://cdn.skypack.dev/fast-copy';
window.fastCopy = fastCopy;
</script>
var MyObject = {
description: 'Creates a deep copy of source, which should be an object or an array.',
myNumber: 123456789,
myBoolean: true,
jayson: {
stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....',
parse: 'JSON.parse() method parses a JSON string...'
}
};
var myCopy = null;
function recursiveDeepCopy(o) {
var newO,
i;
if (typeof o !== 'object') {
return o;
}
if (!o) {
return o;
}
if ('[object Array]' === Object.prototype.toString.apply(o)) {
newO = [];
for (i = 0; i < o.length; i += 1) {
newO[i] = recursiveDeepCopy(o[i]);
}
return newO;
}
newO = {};
for (i in o) {
if (o.hasOwnProperty(i)) {
newO[i] = recursiveDeepCopy(o[i]);
}
}
return newO;
}
myCopy = _.cloneDeep(MyObject);
myCopy = structuredClone(MyObject);
myCopy = recursiveDeepCopy(MyObject);
myCopy = JSON.parse(JSON.stringify(MyObject));
myCopy = fastCopy(MyObject);
function chatGptDeepClone(obj) {
if (obj === null || typeof obj !== 'object') {
return obj;
}
let clone = Array.isArray(obj) ? [] : {};
for (let key in obj) {
if (Object.prototype.hasOwnProperty.call(obj, key)) {
clone[key] = chatGptDeepClone(obj[key]);
}
}
return clone;
}
myCopy = chatGptDeepClone(MyObject);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash cloneDeep | |
Native structuredClone | |
recursiveDeepCopy | |
Json Clone | |
Fast Copy | |
chatGptDeepClone |
Test name | Executions per second |
---|---|
Lodash cloneDeep | 656363.9 Ops/sec |
Native structuredClone | 266193.1 Ops/sec |
recursiveDeepCopy | 905176.3 Ops/sec |
Json Clone | 403752.7 Ops/sec |
Fast Copy | 1110312.6 Ops/sec |
chatGptDeepClone | 609482.2 Ops/sec |
This is a complex benchmark test that compares the performance of different methods for creating deep copies of objects in JavaScript.
Methods being compared:
cloneDeep
function for creating deep copies of objects.structuredClone
function is a new method introduced in ECMAScript 2020, which allows creating deep copies of objects while preserving their structure and semantics.JSON.stringify
and JSON.parse
methods to create a deep copy of an object by serializing it to a string and then parsing it back into an object.Pros and Cons:
Library/Function explanations:
structuredClone
function creates a new object with the same structure and semantics as the original, preserving all its properties and relationships.Special JS feature/syntax:
The benchmark test makes use of ECMAScript 2020 features, specifically the structuredClone
function. This function was introduced as part of the ECMAScript 2020 standard and allows creating deep copies of objects while preserving their structure and semantics.
Alternatives:
Other alternatives for deep copying in JavaScript include:
Object.assign()
can be used to create shallow copies of objects.Keep in mind that the choice of deep copying method depends on specific use cases, performance requirements, and personal preferences.