<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...',
vetor: [
{
stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....',
parse: 'JSON.parse() method parses a JSON string...'
},
{
stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....',
parse: 'JSON.parse() method parses a JSON string...'
},
{
stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....',
parse: 'JSON.parse() method parses a JSON string...'
}
],
jayson: {
stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....',
parse: 'JSON.parse() method parses a JSON string...',
jayson: {
stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....',
parse: 'JSON.parse() method parses a JSON string...',
vetor: [
{
stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....',
parse: 'JSON.parse() method parses a JSON string...'
},
{
stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....',
parse: 'JSON.parse() method parses a JSON string...'
},
{
stringify: 'JSON.stringify() method converts a JavaScript value to a JSON string....',
parse: 'JSON.parse() method parses a JSON string...'
}
],
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 = {MyObject};
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash CloneDeep | |
Json Clone | |
recursiveDeepCopy | |
shalow copy |
Test name | Executions per second |
---|---|
Lodash CloneDeep | 115343.6 Ops/sec |
Json Clone | 80737.5 Ops/sec |
recursiveDeepCopy | 126912.9 Ops/sec |
shalow copy | 3819253.0 Ops/sec |
Let's dive into explaining the provided benchmark.
Benchmark Overview
The test compares the performance of four different methods for creating deep copies of JavaScript objects:
_.cloneDeep
from LodashJSON.parse(JSON.stringify())
recursiveDeepCopy
{...}
Lodash CloneDeep (_ = require('lodash'); var myCopy = _.cloneDeep(MyObject);
)
_.cloneDeep()
function creates a deep copy of the input object, recursively copying all properties and nested objects.JSON.parse(JSON.stringify())
JSON.stringify()
to serialize the input object into a JSON string and then parses it back using JSON.parse()
. Since JavaScript is capable of automatically serializing objects, this method should theoretically create an exact copy.JSON.parse()
will throw an error.recursiveDeepCopy
Shallow Copy (myCopy = {...MyObject};
)
Alternatives
Other methods for creating deep copies of JavaScript objects include:
Object.assign()
: Creates a new object with the same properties as the original input object but without creating new objects for nested values.Array.prototype.slice()
: Creates a shallow copy of an array (not applicable to objects).Keep in mind that the choice of method depends on the specific requirements of your project, such as performance needs, data complexity, and code readability.