Run details:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_2) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.0.4 Safari/605.1.15
Safari 13
Mac OS X 10.15.2
Desktop
4 years ago
Test name Executions per second
immer 747.8 Ops/sec
shallow copy 7290.8 Ops/sec
Immutable 345725.4 Ops/sec
HTML Preparation code:
AخA
 
1
<script src="https://unpkg.com/immer/dist/immer.umd.js"></script>
2
<script src="https://cdnjs.cloudflare.com/ajax/libs/immutable/3.8.2/immutable.js"></script>
Script Preparation code:
x
 
//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 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));