Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Immer (setAutoFreeze(false)) vs shallow vs ramda lens vs cloneDeep of Lodash
(version: 0)
Comparing performance of:
immer vs shallow copy vs Lodash
Created:
5 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://unpkg.com/immer@0.8.0/dist/immer.umd.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.25.0/ramda.min.js"></script> <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.15/lodash.min.js"></script>
Script Preparation code:
var { compose, over, set, lensPath, append } = R var INITIAL_DATA = { items: {}, count: 0, keys: [] } for (var index = 0; index < 100; index++) { INITIAL_DATA[index] = { id: index, name: `ITEM-${index}`, value: Math.random() } INITIAL_DATA.count++ INITIAL_DATA.keys.push(index) } var NEW_ITEM_ID = INITIAL_DATA.count +1 var produce = immer.default immer.setAutoFreeze(false)
Tests:
immer
data = produce(INITIAL_DATA, draft => { draft.items[NEW_ITEM_ID] = { id: NEW_ITEM_ID, name: 'ITEM-NEW', value: 0 } draft.counter++ draft.keys.push(NEW_ITEM_ID) })
shallow copy
data = { ...INITIAL_DATA, items: { ...INITIAL_DATA.items, [NEW_ITEM_ID]: { id: NEW_ITEM_ID, name: 'ITEM-NEW', value: 0 } }, count: INITIAL_DATA.count +1, keys: [ ...INITIAL_DATA.keys, NEW_ITEM_ID] }
Lodash
data = _.cloneDeep(INITIAL_DATA); data.items[NEW_ITEM_ID] = { id: NEW_ITEM_ID, name: 'ITEM-NEW', value: 0 } data.counter++ data.keys.push(NEW_ITEM_ID)
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (3)
Previous results
Fork
Test case name
Result
immer
shallow copy
Lodash
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 provided JSON and explain what's being tested in this JavaScript microbenchmark. **Benchmark Definition** The benchmark compares four approaches to creating a copy of an object: 1. **Immer**: A library that provides a functional programming style for working with immutable data structures. It uses a concept called "drafts" to create temporary, mutable versions of objects during the copying process. 2. **Shallow Copy**: A simple approach that creates a new object and then copies each property from the original object into the new one, without recursively creating new objects for nested properties. 3. **Lodash's `cloneDeep`**: A utility function that creates a deep copy of an object, including all nested properties. **Options Compared** The benchmark compares the performance of these four approaches in terms of: * Creation time: How quickly each approach can create a new copy of the initial data object. * Execution time per iteration: How quickly each approach can execute once, after the initial creation. **Pros and Cons of Each Approach** 1. **Immer**: Pros: * Provides a functional programming style for working with immutable data structures. * Can be more efficient than shallow copying when dealing with large objects. Cons: * Requires a separate library (Immer) to be included. * May incur additional overhead due to the use of drafts. 2. **Shallow Copy**: Pros: * Simple and lightweight approach. * Does not require any additional libraries. Cons: * Can lead to performance issues when dealing with large objects or deeply nested properties. 3. **Lodash's `cloneDeep`**: Pros: + Provides a standardized way to create deep copies of objects across different libraries. + Can be more efficient than manual implementation. Cons: * May incur additional overhead due to the creation of temporary objects. * May not perform as well as custom implementations for very large or complex data structures. **Library Usage** The benchmark uses the following libraries: 1. **Immer**: A library that provides a functional programming style for working with immutable data structures. 2. **Lodash's `cloneDeep`**: A utility function that creates deep copies of objects. These libraries are used to implement the respective copying strategies in each test case. **Special JavaScript Features or Syntax** None mentioned explicitly, but it's worth noting that the benchmark uses ES6 syntax and features (e.g., arrow functions, template literals) which may not be supported by older browsers or environments. **Other Alternatives** For creating copies of objects, other approaches could include: 1. **Manual implementation**: Creating a new object and copying each property manually. 2. **Recursion-based approach**: Using recursion to create a copy of the object, potentially with more efficient handling of nested properties. 3. **Object.assign() or spread operator**: Using these methods to create a shallow copy of an object. However, for large or complex data structures, custom implementations may be necessary to achieve optimal performance.
Related benchmarks:
Immer (setAutoFreeze(false)) vs shallow vs ramda lens vs samless immutable111
Immer (setAutoFreeze(false)) vs shallow vs ramda lens vs immutabe js vs immutable js (toJS) vs mutating 2.0
Immer vs shallow vs ramda lens (2)
Immer (setAutoFreeze(false)) vs shallow vs ramda lens vs samless immutable111 2
Immer (setAutoFreeze(true)) vs shallow vs ramda lens vs samless immutable111 2
Comments
Confirm delete:
Do you really want to delete benchmark?