|
| 1 | +import { produce as produce10, setAutoFreeze as setAutoFreeze10 } from 'immer'; |
| 2 | +import { create as produceMutative } from '../../dist/mutative.esm.mjs'; |
| 3 | +import { bench, run, summary } from 'mitata'; |
| 4 | + |
| 5 | +function createInitialState() { |
| 6 | + const initialState = { |
| 7 | + largeArray: Array.from({ length: 10000 }, (_, i) => ({ |
| 8 | + id: i, |
| 9 | + value: Math.random(), |
| 10 | + nested: { key: `key-${i}`, data: Math.random() }, |
| 11 | + moreNested: { |
| 12 | + items: Array.from({ length: 100 }, (_, i) => ({ |
| 13 | + id: i, |
| 14 | + name: String(i), |
| 15 | + })), |
| 16 | + }, |
| 17 | + })), |
| 18 | + otherData: Array.from({ length: 10000 }, (_, i) => ({ |
| 19 | + id: i, |
| 20 | + name: `name-${i}`, |
| 21 | + isActive: i % 2 === 0, |
| 22 | + })), |
| 23 | + }; |
| 24 | + return initialState; |
| 25 | +} |
| 26 | + |
| 27 | +const MAX = 1; |
| 28 | + |
| 29 | +const add = (index) => ({ |
| 30 | + type: 'test/addItem', |
| 31 | + payload: { id: index, value: index, nested: { data: index } }, |
| 32 | +}); |
| 33 | +const remove = (index) => ({ type: 'test/removeItem', payload: index }); |
| 34 | +const update = (index) => ({ |
| 35 | + type: 'test/updateItem', |
| 36 | + payload: { id: index, value: index, nestedData: index }, |
| 37 | +}); |
| 38 | +const concat = (index) => ({ |
| 39 | + type: 'test/concatArray', |
| 40 | + payload: Array.from({ length: 500 }, (_, i) => ({ id: i, value: index })), |
| 41 | +}); |
| 42 | + |
| 43 | +const actions = { |
| 44 | + add, |
| 45 | + remove, |
| 46 | + update, |
| 47 | + concat, |
| 48 | +}; |
| 49 | + |
| 50 | +const immerProducers = { |
| 51 | + immer10: produce10, |
| 52 | + mutative: produceMutative, |
| 53 | +}; |
| 54 | + |
| 55 | +const setAutoFreezes = { |
| 56 | + vanilla: () => {}, |
| 57 | + immer10: setAutoFreeze10, |
| 58 | + mutative: () => {}, |
| 59 | +}; |
| 60 | + |
| 61 | +const vanillaReducer = (state = createInitialState(), action) => { |
| 62 | + switch (action.type) { |
| 63 | + case 'test/addItem': |
| 64 | + return { |
| 65 | + ...state, |
| 66 | + largeArray: [...state.largeArray, action.payload], |
| 67 | + }; |
| 68 | + case 'test/removeItem': { |
| 69 | + const newArray = state.largeArray.slice(); |
| 70 | + newArray.splice(action.payload, 1); |
| 71 | + return { |
| 72 | + ...state, |
| 73 | + largeArray: newArray, |
| 74 | + }; |
| 75 | + } |
| 76 | + case 'test/updateItem': { |
| 77 | + return { |
| 78 | + ...state, |
| 79 | + largeArray: state.largeArray.map((item) => |
| 80 | + item.id === action.payload.id |
| 81 | + ? { |
| 82 | + ...item, |
| 83 | + value: action.payload.value, |
| 84 | + nested: { ...item.nested, data: action.payload.nestedData }, |
| 85 | + } |
| 86 | + : item |
| 87 | + ), |
| 88 | + }; |
| 89 | + } |
| 90 | + case 'test/concatArray': { |
| 91 | + const length = state.largeArray.length; |
| 92 | + const newArray = action.payload.concat(state.largeArray); |
| 93 | + newArray.length = length; |
| 94 | + return { |
| 95 | + ...state, |
| 96 | + largeArray: newArray, |
| 97 | + }; |
| 98 | + } |
| 99 | + default: |
| 100 | + return state; |
| 101 | + } |
| 102 | +}; |
| 103 | + |
| 104 | +const createImmerReducer = (produce) => { |
| 105 | + const immerReducer = (state = createInitialState(), action) => |
| 106 | + produce(state, (draft) => { |
| 107 | + switch (action.type) { |
| 108 | + case 'test/addItem': |
| 109 | + draft.largeArray.push(action.payload); |
| 110 | + break; |
| 111 | + case 'test/removeItem': |
| 112 | + draft.largeArray.splice(action.payload, 1); |
| 113 | + break; |
| 114 | + case 'test/updateItem': { |
| 115 | + const item = draft.largeArray.find( |
| 116 | + (item) => item.id === action.payload.id |
| 117 | + ); |
| 118 | + item.value = action.payload.value; |
| 119 | + item.nested.data = action.payload.nestedData; |
| 120 | + break; |
| 121 | + } |
| 122 | + case 'test/concatArray': { |
| 123 | + const length = state.largeArray.length; |
| 124 | + const newArray = action.payload.concat(state.largeArray); |
| 125 | + newArray.length = length; |
| 126 | + draft.largeArray = newArray; |
| 127 | + break; |
| 128 | + } |
| 129 | + } |
| 130 | + }); |
| 131 | + |
| 132 | + return immerReducer; |
| 133 | +}; |
| 134 | + |
| 135 | +function mapValues(obj, fn) { |
| 136 | + const result = {}; |
| 137 | + for (const key in obj) { |
| 138 | + result[key] = fn(obj[key]); |
| 139 | + } |
| 140 | + return result; |
| 141 | +} |
| 142 | + |
| 143 | +const reducers = { |
| 144 | + vanilla: vanillaReducer, |
| 145 | + ...mapValues(immerProducers, createImmerReducer), |
| 146 | +}; |
| 147 | + |
| 148 | +function createBenchmarks() { |
| 149 | + for (const action in actions) { |
| 150 | + summary(function () { |
| 151 | + bench(`$action: $version (freeze: $freeze)`, function* (args) { |
| 152 | + const version = args.get('version'); |
| 153 | + const freeze = args.get('freeze'); |
| 154 | + const action = args.get('action'); |
| 155 | + |
| 156 | + const initialState = createInitialState(); |
| 157 | + |
| 158 | + function benchMethod() { |
| 159 | + setAutoFreezes[version](freeze); |
| 160 | + for (let i = 0; i < MAX; i++) { |
| 161 | + reducers[version](initialState, actions[action](i)); |
| 162 | + } |
| 163 | + setAutoFreezes[version](false); |
| 164 | + } |
| 165 | + |
| 166 | + yield benchMethod; |
| 167 | + }).args({ |
| 168 | + version: Object.keys(reducers), |
| 169 | + freeze: [false, true], |
| 170 | + action: [action], |
| 171 | + }); |
| 172 | + }); |
| 173 | + } |
| 174 | +} |
| 175 | + |
| 176 | +async function main() { |
| 177 | + createBenchmarks(); |
| 178 | + await run(); |
| 179 | +} |
| 180 | + |
| 181 | +main(); |
0 commit comments