<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...'
}
};
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 = JSON.parse(JSON.stringify(MyObject));
myCopy = recursiveDeepCopy(MyObject);
myCopy = structuredClone(MyObject);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash CloneDeep | |
Json Clone | |
recursiveDeepCopy | |
structuredClone |
Test name | Executions per second |
---|---|
Lodash CloneDeep | 635433.2 Ops/sec |
Json Clone | 381250.0 Ops/sec |
recursiveDeepCopy | 905076.6 Ops/sec |
structuredClone | 239282.5 Ops/sec |
I'll break down the provided benchmark and explain what's being tested, compared, and the pros/cons of each approach.
Benchmark Overview
The test suite is designed to compare the performance of four different methods for creating a deep copy of an object in JavaScript:
lodash.cloneDeep()
JSON.parse(JSON.stringify())
recursiveDeepCopy()
(a custom implementation)structuredClone()
(a new method introduced in ECMAScript 2020)Comparison of Methods
Each test case uses a similar input object, MyObject
, which contains nested objects and arrays.
lodash.cloneDeep()
: This method is part of the popular JavaScript utility library, Lodash. It recursively creates a deep copy of the original object.JSON.parse(JSON.stringify())
: This method serializes the input object to a JSON string, then parses it back into an object. While this works for simple objects, it can lead to issues with circular references and other complex data structures.recursiveDeepCopy()
: This is a custom implementation of a recursive deep copy function. It handles arrays and objects with nested properties correctly but may be less efficient than the others due to its bespoke nature.structuredClone()
: This method was introduced in ECMAScript 2020 as a new way to create deep copies of objects. It's designed to be more efficient and reliable than traditional methods like JSON.parse(JSON.stringify())
.Pros/Cons of Each Approach
lodash.cloneDeep()
JSON.parse(JSON.stringify())
recursiveDeepCopy()
structuredClone()
Library Used
The lodash.cloneDeep()
method is part of the Lodash library, which provides a wide range of utility functions for JavaScript development. Lodash is widely used and well-maintained, making it a popular choice for many developers.
Special JS Feature/Syntax
None mentioned in this benchmark.
Alternatives
If you need to create deep copies of objects in JavaScript, other alternatives to structuredClone()
include:
JSON.parse(JSON.stringify())
(with caution and awareness of its limitations).recursiveDeepCopy()
.Keep in mind that the choice of method depends on your specific use case, performance requirements, and personal preference.