<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js'></script>
var MyObject = {
"_id": "5af7e5f9c3a3aa2c8495305e",
"index": 0,
"guid": "f4a86db0-f457-400e-9280-e4e84a97191c",
"isActive": true,
"balance": "$1,546.38",
"picture": "http://placehold.it/32x32",
"age": 35,
"eyeColor": "blue",
"name": {
"first": "Trina",
"last": "Bonner"
},
"company": "PHOTOBIN",
"email": "trina.bonner@photobin.co.uk",
"phone": "+1 (907) 417-3108",
"address": "132 Rutherford Place, Munjor, Iowa, 9843",
"about": "Sit consectetur dolor nostrud dolore culpa cupidatat. Commodo minim qui sint est est excepteur est id commodo est ut sunt qui amet. Aute veniam tempor sunt irure nulla.",
"registered": "Tuesday, February 16, 2016 4:23 PM",
"latitude": "-21.702025",
"longitude": "-41.621726",
"tags": [
"consequat",
"nulla",
"aute",
"aute",
"aliqua"
],
"range": [
0,
1,
2,
3,
4,
5,
6,
7,
8,
9
],
"friends": [
{
"id": 0,
"name": "Tommie Noble"
},
{
"id": 1,
"name": "Edna French"
},
{
"id": 2,
"name": "Durham Faulkner"
}
],
"greeting": "Hello, Trina! You have 8 unread messages.",
"favoriteFruit": "strawberry"
};
var myCopy = null;
myCopy = _.cloneDeep(MyObject);
myCopy = JSON.parse(JSON.stringify(MyObject));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
Lodash cloneDeep | |
Json clone |
Test name | Executions per second |
---|---|
Lodash cloneDeep | 26615.9 Ops/sec |
Json clone | 225421.4 Ops/sec |
Let's break down the provided benchmark and explain what's being tested.
Benchmark Definition
The benchmark is comparing two approaches to create a deep copy of an object: using Lodash's cloneDeep
function and using the built-in JSON.parse(JSON.stringify())
method.
Options Compared
cloneDeep
function from the Lodash library, which is a utility library for JavaScript that provides various functions for working with data structures like arrays and objects.JSON.parse(JSON.stringify())
method to create a deep copy of the object.Pros and Cons
JSON.parse(JSON.stringify())
, especially for large objects.cloneDeep
, especially for large objects.Library
The Lodash library is used to provide the cloneDeep
function, which creates a deep copy of an object by recursively cloning all properties of the original object. The JSON.parse(JSON.stringify())
method, on the other hand, uses JavaScript's built-in serialization and deserialization mechanisms to create a shallow copy of an object.
Special JS Feature
There is no special JavaScript feature or syntax being used in this benchmark. Both approaches are straightforward and don't require any advanced features or syntax.
Other Considerations
When deciding between these two approaches, consider the size and complexity of your objects, as well as the trade-offs between performance, ease of use, and additional dependencies.
If you're working with large datasets or complex data structures, cloneDeep
might be a better choice due to its potential for faster execution times. However, if simplicity and minimalism are more important, using JSON.parse(JSON.stringify())
could be a viable option.
Alternatives
Other alternatives for creating deep copies of objects include:
Object.assign()
: While not as efficient as cloneDeep
, Object.assign()
can be used to create a shallow copy of an object.lodash.clone()
: Lodash provides another cloning function called clone()
, which creates a shallow copy of an object.Ultimately, the choice between these approaches depends on your specific needs and priorities.