Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Immer vs Shallow copy vs Immutable Perf Test 2
(version: 0)
Comparing performance of:
immer vs shallow copy vs Immutable
Created:
6 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://unpkg.com/immer/dist/immer.umd.js"></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
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):
Let's dive into the world of JavaScript microbenchmarks. **What is tested?** MeasureThat.net is testing three approaches to update an object in JavaScript: 1. **Immer**: A library that provides a safe and predictable way to mutate objects, similar to how `Array.prototype.push` works. 2. **Shallow copy**: Creating a new object that references the original object's properties, effectively copying its value but not its identity. 3. **Immutable**: Using immutable data structures, where updating an object creates a new one with the updated values. **Options compared** The benchmark is comparing the performance of these three approaches in creating and pushing a new item to an array-like object (`data.items`). The options are: * `immer`: Creates a new draft object and updates it using the `immer` library. * `shallow copy`: Creates a new object that references the original object's properties, effectively copying its value but not its identity. * `Immutable`: Uses immutable data structures, where updating an object creates a new one with the updated values. **Pros and cons of each approach** 1. **Immer**: * Pros: Predictable behavior, safe for concurrent updates, and efficient memory usage. * Cons: Can be slower than shallow copy due to the creation of a new draft object. 2. **Shallow copy**: * Pros: Faster than `immer` since it only creates a reference to the original object's properties. * Cons: Not suitable for concurrent updates or large datasets, as it can lead to memory leaks and inconsistencies. 3. **Immutable**: * Pros: Ensures predictable behavior and thread-safety, making it suitable for concurrent updates and large datasets. * Cons: Can be slower than shallow copy due to the creation of new objects, and may incur additional overhead for object cloning. **Library usage** In this benchmark, `immer` is used as a library to provide a safe and predictable way to mutate objects. The `Immutable.fromJS` function creates an immutable instance from the original data, and `immer.default` is used to create a new draft object. **Special JS feature or syntax** None mentioned in this benchmark. **Other alternatives** In addition to these three approaches, other alternatives for updating JavaScript objects include: * Using `Array.prototype.push` with a loop, which can be slower than shallow copy. * Creating a new object using the spread operator (`{ ...data, items: { ...data.items, [NEW_ITEM_ID]: { id: NEW_ITEM_ID, name: 'ITEM-NEW', value: 0 } } }`) or object destructuring, which can be slower than immutable approaches. Keep in mind that these alternatives may not provide the same level of thread-safety and predictability as `immer` or immutable data structures.
Related benchmarks:
Immer vs Shallow copy vs Immutable Perf Test
Immer vs Shallow copy vs Immutable Perf Test 3
Immerjs vs Shallow copy vs Immutable Perf Test
Immer vs Shallow copy vs Immutable Perf Test updated
Comments
Confirm delete:
Do you really want to delete benchmark?