Test name | Executions per second |
---|---|
immer | 747.8 Ops/sec |
shallow copy | 7290.8 Ops/sec |
Immutable | 345725.4 Ops/sec |
<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));