Toggle navigation
MeasureThat.net
Create a benchmark
Tools
Feedback
FAQ
Register
Log In
Immerjs vs Shallow copy vs Immutable Perf Test
(version: 0)
Comparing performance of:
immer vs shallow copy vs Immutable
Created:
6 years ago
by:
Registered User
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 < 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):
Let's break down the provided benchmark and explain what is tested, compared, and their pros and cons. **Benchmark Overview** The benchmark compares three approaches to create a new immutable object: Immer.js (formerly known as Immutable.js), shallow copy using the spread operator (`...`), and manual modification of an existing object. **Tested Approaches** 1. **Immer.js**: Immer.js is a library that provides a simple and efficient way to work with immutable objects in JavaScript. 2. **Shallow Copy**: Shallow copying involves creating a new object by spreading the properties of an existing object using the spread operator (`...`). 3. **Immutable Approach**: This approach uses manual modification of the original object, where each property is updated individually. **Options Compared** The benchmark compares three options: * `immer.default`: Immer.js provides a default function that creates a new immutable object by creating a draft and updating it. * Spread operator (`...`): Shallow copying involves spreading the properties of an existing object to create a new one. * Manual modification: This approach updates each property individually, rather than creating a new object. **Pros and Cons** 1. **Immer.js (immer.default)**: * Pros: + Efficient and concise way to work with immutable objects. + Provides a consistent API for working with immutable data structures. * Cons: + May have performance overhead due to the creation of a new draft object. 2. **Shallow Copy (using spread operator)`: * Pros: + Simple and easy to implement. + Does not create a new object, which can be beneficial for large datasets. * Cons: + Can lead to performance issues due to the creation of a new object with shallowly copied properties. + May not preserve certain property types (e.g., functions). 3. **Manual Modification**: * Pros: + No overhead in terms of creating new objects. * Cons: + Requires manual updates, which can lead to errors and maintenance issues. **Library: Immutable** The library used is Immutable.js, now known as Immer.js. It provides a simple and efficient way to work with immutable objects in JavaScript. The `Immutable.fromJS` function creates a new immutable object from a given JavaScript object, and the `Immer.set` and `Immer.get` functions are used to update and retrieve properties of an immutable object. **Special JS Feature: Shorthand Property Access** The benchmark uses shorthand property access (`data[index] = { id: index, name: `ITEM-${index}`, value: Math.random() }`) in the JavaScript code. This feature allows accessing properties using square brackets `[]`, which is a common pattern in JavaScript.
Related benchmarks:
Immer vs Shallow copy vs Immutable Perf Test
Immer vs Shallow copy vs Immutable Perf Test 2
Immer vs Shallow copy vs Immutable Perf Test 3
Immer vs Shallow copy vs Immutable Perf Test updated
Comments
Confirm delete:
Do you really want to delete benchmark?