<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
var MyObject = [{
"id": 62,
"name": "option 1 test",
"address": {
"id": 62,
"name": null,
"addressLine2": null,
"addressLine1": null,
"city": "LONDON",
"country": null,
"postal": null,
"latitude": null,
"longitude": null,
"description": null,
"transport": [{
"id": 2,
"description": "instrcution bus 1",
"type": "BUS"
}],
"contact": "",
"media": [{
"id": 97,
"url": "https://headout-test.s3.us-west-2.amazonaws.com/media/Nitisha Chhatwani Resume (1)-2 (2).pdf"
},
{
"id": 98,
"url": "https://headout-test.s3.us-west-2.amazonaws.com/media/Nitisha Chhatwani Resume (1)-2 (2).pdf"
},
{
"id": 99,
"url": "https://headout-test.s3.us-west-2.amazonaws.com/media/Nitisha Chhatwani Resume (1)-2 (2).pdf"
},
{
"id": 100,
"url": "https://headout-test.s3.us-west-2.amazonaws.com/media/Nitisha Chhatwani Resume (1)-2 (2).pdf"
},
{
"id": 101,
"url": "https://headout-test.s3.us-west-2.amazonaws.com/media/Nitisha Chhatwani Resume (1)-2 (2).pdf"
}
]
},
"redemptionAddress": null,
"referenceCode": null,
"inclusions": [],
"exclusions": [],
"hotelTransfer": null,
"hotels": null,
"isCancellable": null,
"canReschedule": null,
"isRedemptionMeetingPointSame": null,
"offlineRedemption": null,
"fulfilmentType": null,
"freesaleOptionSetting": null,
"apiOptionSetting": null,
"onRequestOptionSetting": null,
"portalOptionSetting": null,
"periodType": null,
"periodDurationHours": 0,
"periodDurationMinutes": 0,
"cancellationDuration": null,
"rescheduleDuration": null,
"paxSettings": [{
"id": 2,
"ageFrom": 1,
"ageTo": 2,
"description": "",
"paxType": "INFANT"
}]
},
{
"id": 136,
"name": null,
"address": {
"id": 142,
"name": null,
"addressLine2": null,
"addressLine1": null,
"city": "LONDON",
"country": null,
"postal": null,
"latitude": null,
"longitude": null,
"description": null,
"transport": [],
"contact": "",
"media": []
},
"redemptionAddress": null,
"referenceCode": null,
"inclusions": [],
"exclusions": [],
"hotelTransfer": null,
"hotels": null,
"isCancellable": null,
"canReschedule": null,
"isRedemptionMeetingPointSame": null,
"offlineRedemption": null,
"fulfilmentType": null,
"freesaleOptionSetting": null,
"apiOptionSetting": null,
"onRequestOptionSetting": null,
"portalOptionSetting": null,
"periodType": null,
"periodDurationHours": 0,
"periodDurationMinutes": 0,
"cancellationDuration": null,
"rescheduleDuration": null,
"paxSettings": [{
"id": 63,
"ageFrom": 1,
"ageTo": 2,
"description": "",
"paxType": "INFANT"
}]
}
];
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 | 79389.3 Ops/sec |
Json Clone | 91579.3 Ops/sec |
I'll break down the provided benchmark definition and explain what's being tested, the options compared, their pros and cons, and other considerations.
Benchmark Definition
The test consists of two individual benchmark cases:
Options Compared
For each benchmark case, the following options are compared:
a. Lodash CloneDeep
* Option: lodash.cloneDeep(MyObject)
* Purpose: Deeply clone the MyObject
JSON object using Lodash's cloneDeep
function.
b. Json Clone
* Option: JSON.parse(JSON.stringify(MyObject))
Pros and Cons
a. Lodash CloneDeep
Pros:
cloneDeep
ensures that the cloned object is an independent copy, which can prevent unexpected behavior or side effects.Cons:
b. Json Clone
Pros:
JSON.parse(JSON.stringify(MyObject))
is a straightforward and well-known method for cloning JSON objects.cloneDeep
due to its simplicity and lack of overhead.Cons:
Other Considerations
In summary, Lodash's cloneDeep
provides safety guarantees by creating an independent copy of the object, but comes with a performance overhead due to the library's complexity. The JSON.parse(JSON.stringify(MyObject))
method is simpler and faster but may not handle circular references or retain metadata.