<script src='https://cdn.jsdelivr.net/npm/lodash@4.17.21/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);
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash CloneDeep | |
Json Clone | |
recursiveDeepCopy |
Test name | Executions per second |
---|---|
Lodash CloneDeep | 1451799.8 Ops/sec |
Json Clone | 756593.0 Ops/sec |
recursiveDeepCopy | 1982047.0 Ops/sec |
Let's break down the benchmark and explain what's being tested.
Benchmark Overview
The benchmark compares three approaches to create a deep copy of an object: _.cloneDeep
from Lodash, JSON.parse(JSON.stringify())
, and a custom recursive function called recursiveDeepCopy
. The benchmark is designed to test which approach is the fastest for creating deep copies.
Test Cases
_
(Lodash) utility library to create a deep copy of the input object using the _.cloneDeep()
method.JSON.parse(JSON.stringify())
to create a deep copy of the input object.Library: Lodash
Lodash is a popular utility library for JavaScript that provides a wide range of functions and helpers for tasks like array manipulation, object manipulation, and more. The _
variable in the benchmark refers to the entire Lodash library, which is imported using the https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js
URL.
JSON.parse(JSON.stringify())
The JSON.parse(JSON.stringify())
approach uses the built-in JSON serialization and deserialization functions in JavaScript to create a deep copy of the input object. This method is simple but can be slower than other approaches, especially for large objects with complex relationships.
recursiveDeepCopy
The custom recursiveDeepCopy
function uses recursion to create a deep copy of the input object. This approach is more explicit and allows for fine-grained control over the copying process. However, it may be slower due to the overhead of recursive function calls.
Pros and Cons
Here's a brief summary of the pros and cons of each approach:
Other Alternatives
If you're looking for alternative deep copy libraries or functions in JavaScript, here are a few options:
_.cloneDeep()
method from Lodash.Note that the choice of deep copy library or function ultimately depends on your specific use case and performance requirements.