Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Immer vs cloneDeep()
(version: 0)
Comparing performance of:
Immer vs cloneDeep
Created:
2 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/immer@3.1.3/dist/immer.umd.min.js"></script> <script src='https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.5/lodash.min.js'></script> <script src="https://cdn.jsdelivr.net/npm/uuid@8.3.2/dist/umd/uuidv4.min.js"></script>
Script Preparation code:
state = { "id": "2206513", "type": "HOME_BLADDER_DIARY", "status": "STARTED", "cycles": [ { "id": "2206514", "index": 1, "number": 1, "status": "COMPLETED", "eventIds": [ "2206517", "2206533", "2206525" ], "patientEvaluation": "COMPLETE", "startDate": "2023-11-13T15:03:55.312+01:00", "finished": false }, { "id": "2206515", "index": 2, "number": 2, "status": "STARTED", "eventIds": [ "2206544", "2206547", "2206599", "2206550", "2206553", "2206541", "2206556", "2206559", "2206607" ], "patientEvaluation": null, "startDate": "2023-11-15T09:36:28.470Z", "finished": false } ], "medicalReason": null, "medication": null, "notes": null, "management": null, "amountOfFlows": null, "patientNotes": null, "usualWakeTime": "07:00", "usualSleepTime": "23:00", "catheterization": "NEVER", "amountOfCycles": 2, "volumeHidden": false, "forceConsecutiveCycles": true }
Tests:
Immer
const result = immer.produce(state, draft => { draft.cycles[0].eventIds[3] = 'POTATO' draft.cycles[0].eventIds[2] = 'POTATO' draft.cycles[0].eventIds[1] = 'POTATO' draft.cycles[0].eventIds[0] = 'POTATO' })
cloneDeep
let result = _.cloneDeep(state); result.cycles[0].eventIds[3] = 'POTATO'; result = _.cloneDeep(state); result.cycles[0].eventIds[2] = 'POTATO'; result = _.cloneDeep(state); result.cycles[0].eventIds[1] = 'POTATO'; result = _.cloneDeep(state); result.cycles[0].eventIds[0] = 'POTATO';
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (2)
Previous results
Fork
Test case name
Result
Immer
cloneDeep
Fastest:
N/A
Slowest:
N/A
Latest run results:
No previous run results
This benchmark does not have any results yet. Be the first one
to run it!
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
Let's break down the benchmark and its components. **Benchmark Definition** The benchmark is designed to test two approaches: using `Immer` for state management and using `lodash.cloneDeep` for deep cloning objects. **Options being compared** Two options are being compared: 1. **Immer**: Immer is a library that provides a safe and efficient way to update complex data structures, such as arrays and objects, while preserving the original state. 2. **cloneDeep**: cloneDeep is a function from the Lodash library that creates a deep copy of an object, allowing for changes to be made to the cloned object without affecting the original. **Pros and Cons** Here are some pros and cons of each approach: * **Immer**: + Pros: Immer provides a safe and efficient way to update complex data structures. It also preserves the original state, making it easier to debug and test. + Cons: Immer requires an understanding of its API and can be more verbose than other cloning libraries. * **cloneDeep**: + Pros: cloneDeep is simple to use and provides a basic deep cloning functionality. + Cons: Using `cloneDeep` can lead to slower performance compared to Immer, as it creates new objects on every iteration. **Other considerations** When using `Immer`, it's essential to note that it returns a new draft object, which is then committed back to the original state. This approach ensures that the original state remains unchanged, making it easier to test and debug. On the other hand, when using `cloneDeep`, it creates a deep copy of the original object, allowing for changes to be made to the cloned object without affecting the original. However, this approach can lead to slower performance compared to Immer. **Library explanations** * **Immer**: Immer is a JavaScript library developed by Tyler McGinnis and Aaron Hill. It provides a safe and efficient way to update complex data structures while preserving the original state. * **Lodash.cloneDeep**: Lodash's `cloneDeep` function creates a deep copy of an object, allowing for changes to be made to the cloned object without affecting the original. **Special JS feature or syntax** There are no special JS features or syntax used in this benchmark. The code is straightforward and focuses on comparing two different state management approaches. Now, let's discuss some alternative approaches that could have been used: * **Mocha**: Mocha is a popular testing framework for Node.js and JavaScript applications. It provides a lot of flexibility and customization options for writing tests. * **Jest**: Jest is another popular testing framework developed by Facebook. It provides a lot of features out of the box, including code coverage analysis and snapshot testing. * **Manual cloning**: If you didn't want to use an external library like Lodash or Immer, you could have manually cloned objects using techniques like `JSON.parse(JSON.stringify(obj))` or using libraries like `lodash.merge`. However, this approach can be more error-prone and less efficient. Overall, the benchmark provides a simple and clear way to compare two state management approaches: Immer and cloneDeep.
Related benchmarks:
Lodash cloneDeep vs JSON Clonef
Lodash cloneDeep vs JSON Clone (large payload)
Lodash cloneDeep vs JSON Clone vs Ramda Clone for big Array
Immer vs cloneDeep
Comments
Confirm delete:
Do you really want to delete benchmark?