<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var MyObject = {
"_id": "5d89d740e050f13d5b65c172",
"index": 0,
"guid": "85ed8578-6498-4b91-bc46-71a0e56e0ea2",
"isActive": false,
"balance": "$3,626.01",
"picture": "http://placehold.it/32x32",
"age": 366,
"eyeColor": "green",
"name": "Oneil Byrd",
"gender": "male",
"company": "CEPRENE",
"email": "oneilbyrd@ceprene.com",
"phone": "+1 (929) 467-2238",
"address": "688 Dunne Place, Walker, Rhode Island, 2210",
"about": "Occaecat qui sit do reprehenderit ex proident excepteur officia adipisicing id do. Ut non ipsum ullamco reprehenderit duis mollit culpa adipisicing. Velit Lorem cupidatat incididunt cillum aliquip. Minim elit enim laboris duis deserunt incididunt culpa minim nisi.\r\n",
"registered": "2017-04-19T11:07:55 -02:00",
"latitude": 87.086108,
"longitude": -35.085309,
"tags": [
"irure",
"in",
"ex",
"eu",
"id",
"incididunt",
"officia",
"est",
"id",
"occaecat",
"irure",
"labore",
"commodo",
"adipisicing",
"mollit",
"esse",
"do",
"pariatur",
"duis",
"dolore",
"ipsum",
"voluptate",
"nulla",
"cupidatat",
"dolore",
"veniam",
"ea"
],
"friends": [
{
"id": 0,
"name": "Minerva Hancock"
},
{
"id": 1,
"name": "Lucille Mckinney"
},
{
"id": 2,
"name": "Ursula Cummings"
},
{
"id": 3,
"name": "Juana Mann"
},
{
"id": 4,
"name": "Hubbard Martin"
},
{
"id": 5,
"name": "Cheryl Duffy"
},
{
"id": 6,
"name": "Workman Duran"
},
{
"id": 7,
"name": "Geneva Randolph"
},
{
"id": 8,
"name": "Nelson York"
},
{
"id": 9,
"name": "Powers Wyatt"
},
{
"id": 10,
"name": "Sophia Gilliam"
},
{
"id": 11,
"name": "Kidd Eaton"
},
{
"id": 12,
"name": "Caitlin Wall"
}
],
"greeting": "Hello, Oneil Byrd! You have 9 unread messages.",
"favoriteFruit": "apple"
};
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 | 83388.2 Ops/sec |
Json clone | 94136.6 Ops/sec |
Let's dive into the explanation of the provided benchmark.
Benchmark Definition and Preparation
The benchmark defines two test cases:
cloneDeep
function to create a deep copy of the MyObject
JSON object.JSON.parse(JSON.stringify(MyObject))
method to clone the MyObject
JSON object.Options Compared
The two options being compared are:
cloneDeep
functionJSON.parse(JSON.stringify(MyObject))
methodPros and Cons of Each Approach
Lodash CloneDeep
Pros:
Cons:
JSON Clone
Pros:
Cons:
cloneDeep
function due to its implementation details.Library: Lodash
Lodash is a popular JavaScript library that provides a collection of utility functions. In this case, the cloneDeep
function is used to create a deep copy of the MyObject
JSON object. Lodash is widely adopted and well-maintained, making it a reliable choice for many developers.
Special JS Feature: None
There are no special JavaScript features or syntax being tested in this benchmark. The focus is on comparing two specific implementation options.
Other Alternatives
If you wanted to test alternative cloning methods, some other options could be:
JSON.parse(JSON.stringify(MyObject))
with a recursive function (e.g., using a library like Underscore.js)However, for most use cases, Lodash's cloneDeep
function and the built-in JSON.parse(JSON.stringify(MyObject))
method are likely to be sufficient.