Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
lodash clonedeep vs json.parse(stringify())
(version: 0)
Comparing performance of:
Lodash CloneDeep vs Json Clone
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script>
Script Preparation code:
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;
Tests:
Lodash CloneDeep
myCopy = _.cloneDeep(MyObject);
Json Clone
myCopy = JSON.parse(JSON.stringify(MyObject));
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Lodash CloneDeep
Json Clone
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
3 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/140.0.0.0 Safari/537.36 Edg/140.0.0.0
Browser/OS:
Chrome 140 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
Lodash CloneDeep
50966.2 Ops/sec
Json Clone
80624.2 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
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: 1. "Lodash CloneDeep" 2. "Json Clone" **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: 1. Performance: Cloning a deeply nested object can be slower than other methods due to the overhead of recursively iterating over the object's properties. 2. Safety: Using `cloneDeep` ensures that the cloned object is an independent copy, which can prevent unexpected behavior or side effects. Cons: 1. Overhead: The Lodash library needs to be included in the test environment, which can introduce additional dependencies and potential performance overhead. 2. Complexity: Cloning a deeply nested object can lead to complex code, making it harder to debug and maintain. b. **Json Clone** Pros: 1. Simplicity: Using `JSON.parse(JSON.stringify(MyObject))` is a straightforward and well-known method for cloning JSON objects. 2. Performance: This method is generally faster than using Lodash's `cloneDeep` due to its simplicity and lack of overhead. Cons: 1. Inability to handle circular references: If the original object contains circular references (e.g., an object that references itself), the cloned object will also contain these references, leading to infinite recursion. 2. Loss of metadata: The cloned object will not retain the same metadata as the original object (e.g., timestamps or other non-numeric properties). **Other Considerations** 1. **Caching**: Both methods may benefit from caching intermediate results or using memoization to avoid redundant calculations. 2. **Browser-specific optimizations**: Some browsers, like Chrome, have built-in optimizations for JSON parsing and cloning that might affect the benchmark results. 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.
Related benchmarks:
Lodash benchmark (Anton)
Lodash cloneDeep vs JSON Clone with Array
cloneDeep vs JSON stringify + parse (long arr)
lodash cloneDeep vs json.stringify
Lodash cloneDeep vs JSON parse
Comments
Confirm delete:
Do you really want to delete benchmark?