Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Immer vs Shallow copy vs Immutable Perf Test copy
(version: 0)
Comparing performance of:
immer vs shallow copy vs Immutable
Created:
4 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/immer"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.js"></script>
Script Preparation code:
//const { fromJS } = require('immutable'); var data = { items: {}, count: 0, keys: [] } for (let index = 0; index < 100; index++) { data[index] = { id: index, name: `ITEM-${index}`, value: Math.random() } data.count++ data.keys.push(index) } var NEW_ITEM_ID = data.count +1 var produce = immer.default var im = Immutable.fromJS(data);
Tests:
immer
data = produce(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 = { ...data, items: { ...data.items, [NEW_ITEM_ID]: { id: NEW_ITEM_ID, name: 'ITEM-NEW', value: 0 } }, count: data.count +1, keys: [ ...data.keys, NEW_ITEM_ID] }
Immutable
var im = Immutable.fromJS(data); im = im.set('items', im.get('items').set(NEW_ITEM_ID, { id: NEW_ITEM_ID, name: 'ITEM-NEW', value: 0 })); im = im.set('count', im.count +1); im = im.set('keys', im.get('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
Immutable
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):
**Overview of the Benchmark** The provided benchmark compares the performance of three approaches to update an object: Immutable, Shallow Copy, and Immutable (again). The benchmark is designed to test how efficient each approach is in updating a nested object structure. **Approaches Compared** 1. **Immutable**: This approach uses the Immutable library to create a new, immutable copy of the original object and updates it using the `set` method. 2. **Shallow Copy**: This approach creates a shallow copy of the original object by spreading its properties and then updates the copied object with new values. **Pros and Cons** * **Immutable**: + Pros: Ensures data consistency and immutability, which can be beneficial for concurrent programming or when working with complex, stateful applications. + Cons: Creates a new object, which can lead to increased memory usage and slower performance compared to other approaches. * **Shallow Copy**: + Pros: Faster and more memory-efficient than Immutable, as it only creates a copy of the top-level object properties. + Cons: Can lead to unexpected behavior if the original object is modified concurrently or if the copied object's structure changes unexpectedly. **Library and Syntax Explanation** The benchmark uses the following libraries: * **Immer**: A library for working with immutable data structures. It provides methods like `set` and `get` to update and access objects in a safe, predictable way. * **Immutable**: The Immutable library itself is used as an alternative name for the same functionality provided by Immer. **Special JS Features/Syntax** The benchmark uses the following special JavaScript features: * **Spread operator (`...`)**: Used to create shallow copies of object properties and arrays. * **Template literals (`\t`)**: Used to create string values with indentation. **Other Alternatives** Other approaches that could be used to update an object include: * **Deep Copy**: Creating a deep copy of the original object using a library like Lodash's `cloneDeep` function or implementing your own recursive copying logic. * **Object Assignment**: Directly assigning new values to existing object properties, which can lead to unexpected behavior if not done carefully. In conclusion, the benchmark provides a useful comparison between three approaches to updating an object in JavaScript, highlighting the trade-offs between immutability, memory efficiency, and performance.
Related benchmarks:
Immer vs Shallow copy vs Immutable Perf Test
Immerjs vs Shallow copy vs Immutable Perf Test
Immer vs Shallow copy vs Immutable Perf Test updated
Immerjs vs Immutable vs Shallow copy vs Object.assign (performance test) 3
Comments
Confirm delete:
Do you really want to delete benchmark?