<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>
//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);
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)
})
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]
}
var um = im.set('items', im.get('items').set(NEW_ITEM_ID, { id: NEW_ITEM_ID, name: 'ITEM-NEW', value: 0 }));
um = um.set('count', um.count +1);
um = um.set('keys', um.get('keys').push(NEW_ITEM_ID));
--enable-precise-memory-info
flag.
Test case name | Result |
---|---|
immer | |
shallow copy | |
Immutable |
Test name | Executions per second |
---|---|
immer | 747.8 Ops/sec |
shallow copy | 7290.8 Ops/sec |
Immutable | 345725.4 Ops/sec |
I'll provide an explanation of the benchmark, its options, pros and cons, and other considerations.
Benchmark Overview
The benchmark measures the performance of three approaches to creating a new item in an object: Immutable.js (also known as "Immer"), shallow copy using the spread operator (...
), and mutable assignment (var um = im.set()
).
Options Compared
immer.default
function to create a draft object and then updates it with the new values....
): This approach creates a shallow copy of the original data structure by spreading its properties into a new object. When creating a new item, it adds the new property to the existing object using the syntax data.items[NEW_ITEM_ID] = { ... }
.var um = im.set()
): This approach creates a mutable copy of the original data structure by updating an existing object directly.Pros and Cons
...
):var um = im.set()
):Other Considerations
...
) might be a good trade-off between performance and ease of implementation.var um = im.set()
) is generally not recommended due to its potential side effects and lack of predictability.Libraries Used
immer.default
, which creates a draft object, and set
/get
methods for updating and accessing objects....
): A built-in JavaScript operator that creates a shallow copy of an object.Special JS Features or Syntax
This benchmark does not explicitly use any special JavaScript features or syntax, such as async/await, Promises, or Web Workers. However, the benchmark uses the for
loop to create the initial data structure, which might be optimized for older browsers using techniques like memoization or caching.
Please note that this explanation provides a general overview of the benchmark and its options. Depending on specific use cases and requirements, different approaches might be more suitable.