-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
perf(array): improve array remove method perf
- Loading branch information
Showing
6 changed files
with
229 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,181 @@ | ||
import { produce as produce10, setAutoFreeze as setAutoFreeze10 } from 'immer'; | ||
import { create as produceMutative } from '../../dist/mutative.esm.mjs'; | ||
import { bench, run, summary } from 'mitata'; | ||
|
||
function createInitialState() { | ||
const initialState = { | ||
largeArray: Array.from({ length: 10000 }, (_, i) => ({ | ||
id: i, | ||
value: Math.random(), | ||
nested: { key: `key-${i}`, data: Math.random() }, | ||
moreNested: { | ||
items: Array.from({ length: 100 }, (_, i) => ({ | ||
id: i, | ||
name: String(i), | ||
})), | ||
}, | ||
})), | ||
otherData: Array.from({ length: 10000 }, (_, i) => ({ | ||
id: i, | ||
name: `name-${i}`, | ||
isActive: i % 2 === 0, | ||
})), | ||
}; | ||
return initialState; | ||
} | ||
|
||
const MAX = 1; | ||
|
||
const add = (index) => ({ | ||
type: 'test/addItem', | ||
payload: { id: index, value: index, nested: { data: index } }, | ||
}); | ||
const remove = (index) => ({ type: 'test/removeItem', payload: index }); | ||
const update = (index) => ({ | ||
type: 'test/updateItem', | ||
payload: { id: index, value: index, nestedData: index }, | ||
}); | ||
const concat = (index) => ({ | ||
type: 'test/concatArray', | ||
payload: Array.from({ length: 500 }, (_, i) => ({ id: i, value: index })), | ||
}); | ||
|
||
const actions = { | ||
add, | ||
remove, | ||
update, | ||
concat, | ||
}; | ||
|
||
const immerProducers = { | ||
immer10: produce10, | ||
mutative: produceMutative, | ||
}; | ||
|
||
const setAutoFreezes = { | ||
vanilla: () => {}, | ||
immer10: setAutoFreeze10, | ||
mutative: () => {}, | ||
}; | ||
|
||
const vanillaReducer = (state = createInitialState(), action) => { | ||
switch (action.type) { | ||
case 'test/addItem': | ||
return { | ||
...state, | ||
largeArray: [...state.largeArray, action.payload], | ||
}; | ||
case 'test/removeItem': { | ||
const newArray = state.largeArray.slice(); | ||
newArray.splice(action.payload, 1); | ||
return { | ||
...state, | ||
largeArray: newArray, | ||
}; | ||
} | ||
case 'test/updateItem': { | ||
return { | ||
...state, | ||
largeArray: state.largeArray.map((item) => | ||
item.id === action.payload.id | ||
? { | ||
...item, | ||
value: action.payload.value, | ||
nested: { ...item.nested, data: action.payload.nestedData }, | ||
} | ||
: item | ||
), | ||
}; | ||
} | ||
case 'test/concatArray': { | ||
const length = state.largeArray.length; | ||
const newArray = action.payload.concat(state.largeArray); | ||
newArray.length = length; | ||
return { | ||
...state, | ||
largeArray: newArray, | ||
}; | ||
} | ||
default: | ||
return state; | ||
} | ||
}; | ||
|
||
const createImmerReducer = (produce) => { | ||
const immerReducer = (state = createInitialState(), action) => | ||
produce(state, (draft) => { | ||
switch (action.type) { | ||
case 'test/addItem': | ||
draft.largeArray.push(action.payload); | ||
break; | ||
case 'test/removeItem': | ||
draft.largeArray.splice(action.payload, 1); | ||
break; | ||
case 'test/updateItem': { | ||
const item = draft.largeArray.find( | ||
(item) => item.id === action.payload.id | ||
); | ||
item.value = action.payload.value; | ||
item.nested.data = action.payload.nestedData; | ||
break; | ||
} | ||
case 'test/concatArray': { | ||
const length = state.largeArray.length; | ||
const newArray = action.payload.concat(state.largeArray); | ||
newArray.length = length; | ||
draft.largeArray = newArray; | ||
break; | ||
} | ||
} | ||
}); | ||
|
||
return immerReducer; | ||
}; | ||
|
||
function mapValues(obj, fn) { | ||
const result = {}; | ||
for (const key in obj) { | ||
result[key] = fn(obj[key]); | ||
} | ||
return result; | ||
} | ||
|
||
const reducers = { | ||
vanilla: vanillaReducer, | ||
...mapValues(immerProducers, createImmerReducer), | ||
}; | ||
|
||
function createBenchmarks() { | ||
for (const action in actions) { | ||
summary(function () { | ||
bench(`$action: $version (freeze: $freeze)`, function* (args) { | ||
const version = args.get('version'); | ||
const freeze = args.get('freeze'); | ||
const action = args.get('action'); | ||
|
||
const initialState = createInitialState(); | ||
|
||
function benchMethod() { | ||
setAutoFreezes[version](freeze); | ||
for (let i = 0; i < MAX; i++) { | ||
reducers[version](initialState, actions[action](i)); | ||
} | ||
setAutoFreezes[version](false); | ||
} | ||
|
||
yield benchMethod; | ||
}).args({ | ||
version: Object.keys(reducers), | ||
freeze: [false, true], | ||
action: [action], | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
async function main() { | ||
createBenchmarks(); | ||
await run(); | ||
} | ||
|
||
main(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5412,6 +5412,11 @@ minimist@^1.2.0, minimist@^1.2.5, minimist@^1.2.6: | |
resolved "https://registry.yarnpkg.com/minimist/-/minimist-1.2.8.tgz#c1a464e7693302e082a075cee0c057741ac4772c" | ||
integrity sha512-2yyAR8qBkN3YuheJanUpWC5U3bb5osDywNB8RzDVlDwDHbocAJveqqj1u8+SVD7jkWT4yvsHCpWqqWqAxb0zCA== | ||
|
||
mitata@^1.0.25: | ||
version "1.0.25" | ||
resolved "https://registry.yarnpkg.com/mitata/-/mitata-1.0.25.tgz#918d0d04d2be0aeae7152cc7d8373b3a727b1b4f" | ||
integrity sha512-0v5qZtVW5vwj9FDvYfraR31BMDcRLkhSFWPTLaxx/Z3/EvScfVtAAWtMI2ArIbBcwh7P86dXh0lQWKiXQPlwYA== | ||
|
||
[email protected]: | ||
version "2.1.2" | ||
resolved "https://registry.yarnpkg.com/ms/-/ms-2.1.2.tgz#d09d1f357b443f493382a8eb3ccd183872ae6009" | ||
|