Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Immerjs vs Immutable vs Shallow copy vs Object.assign (performance test)
(version: 0)
Comparing performance of:
immer vs Immutable vs Shallow copy vs Object.assign
Created:
3 years ago
by:
Guest
Jump to the latest result
HTML Preparation code:
<script src="https://cdn.jsdelivr.net/npm/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 < 100000; 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) })
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));
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] }
Object.assign
data = Object.assign(data, { ...data, items: Object.assign(data.items, { [NEW_ITEM_ID]: { id: NEW_ITEM_ID, name: 'ITEM-NEW', value: 0 }, }), count: data.count + 1, keys: [...data.keys, NEW_ITEM_ID] })
Rendered benchmark preparation results:
Suite status:
<idle, ready to run>
Run tests (4)
Previous results
Fork
Test case name
Result
immer
Immutable
Shallow copy
Object.assign
Fastest:
N/A
Slowest:
N/A
Latest run results:
Run details:
(Test run date:
10 months ago
)
User agent:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/138.0.0.0 Safari/537.36
Browser/OS:
Chrome 138 on Windows
View result in a separate tab
Embed
Embed Benchmark Result
Test name
Executions per second
immer
23.1 Ops/sec
Immutable
8.2 Ops/sec
Shallow copy
2055.9 Ops/sec
Object.assign
70.6 Ops/sec
Autogenerated LLM Summary
(model
llama3.2:3b
, generated one year ago):
This is a JavaScript performance benchmarking test created using MeasureThat.net. The test aims to compare the performance of four different approaches for updating an object's properties. **Benchmark Definition** The benchmark definition defines a sample dataset `data` with 100,000 items, each containing an `id`, `name`, and `value`. The test also introduces a new item ID `NEW_ITEM_ID` that will be used to update the existing data. **Options Compared** The four options compared in this benchmark are: 1. **Immer**: A library for functional immutable object updates. 2. **Immutable**: A library for creating and manipulating immutable objects. 3. **Shallow copy**: Creating a shallow copy of the original object using the spread operator (`{...data}`). 4. **Object.assign**: Using the `Object.assign` method to update the object's properties. **Pros and Cons** 1. **Immer**: * Pros: Fast updates, efficient for large datasets. * Cons: Requires a separate library, can be less intuitive for beginners. 2. **Immutable**: * Pros: Provides strong guarantees about data immutability, suitable for concurrent programming. * Cons: Can result in slower performance compared to Immer, more complex usage. 3. **Shallow copy**: * Pros: Simple and widely supported, can be useful for small datasets. * Cons: May lead to unnecessary memory allocations, not efficient for large datasets. 4. **Object.assign**: * Pros: Widely supported and understood, easy to use. * Cons: Can result in slower performance compared to Immer and Immutable, may overwrite existing data. **Library Usage** In the benchmark definition, two libraries are used: 1. **Immer**: The `immer.default` function is used to create a draft object that can be updated. 2. **Immutable**: The `Immutable.fromJS` function is used to create an immutable object from the original dataset. **Special JS Feature/ Syntax** This benchmark does not use any special JavaScript features or syntax beyond what's commonly available in modern browsers. **Alternatives** Other alternatives for updating objects include: 1. Using the spread operator (`{...data}`) with `Object.create` to create a new object. 2. Using `Array.prototype.slice()` and `Object.assign` to create a shallow copy of an array-like object. 3. Using a custom recursive function to update nested objects. Keep in mind that these alternatives may have different performance characteristics and use cases compared to the options tested in this benchmark.
Related benchmarks:
Immer vs shallow copy vs Immutable
Immerjs vs Shallow copy vs Immutable Perf Test
Immerjs vs Immutable vs Shallow copy vs Object.assign (performance test) 2
Immerjs vs Immutable vs Shallow copy vs Object.assign (performance test) 3
Comments
Confirm delete:
Do you really want to delete benchmark?