<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></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...',
object: {
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...',
object: {
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...',
object: {
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...',
object: {
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...',
object: {
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));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash | |
structuredClone | |
recursiveDeepCopy | |
Json clone |
Test name | Executions per second |
---|---|
Lodash | 211190.8 Ops/sec |
structuredClone | 127802.0 Ops/sec |
recursiveDeepCopy | 227261.9 Ops/sec |
Json clone | 129593.4 Ops/sec |
Measuring the performance of different methods for deep cloning objects in JavaScript is an essential task, especially when working with complex data structures.
Benchmark Definition
The benchmark definition provided measures the performance of four different methods for creating a deep copy of a JavaScript object:
Lodash.cloneDeep
structuredClone
recursiveDeepCopy
(a custom implementation)JSON.parse(JSON.stringify())
Options Compared
Each option has its own strengths and weaknesses:
Pros and Cons
Here's a brief summary of each option:
Map
or Set
).Benchmark Results
The latest benchmark results show that:
structuredClone
is the fastest option, followed closely by Lodash.cloneDeep
.recursiveDeepCopy
is slower than both Lodash and structured Clone.JSON.parse(JSON.stringify())
is the slowest option due to its inefficiencies.In conclusion, for most use cases, structuredClone is the recommended choice for deep cloning objects in JavaScript, thanks to its speed, efficiency, and safety features.