perf: remove extra spread operators from the combine maps function

This commit is contained in:
Moody Salem 2021-07-23 09:27:23 -05:00
parent 634d010d92
commit 3686506f2a
No known key found for this signature in database
GPG Key ID: 8CB5CD10385138DB

@ -47,16 +47,29 @@ export function useAllLists(): AppState['lists']['byUrl'] {
return useAppSelector((state) => state.lists.byUrl) return useAppSelector((state) => state.lists.byUrl)
} }
// TODO: return a proxy instead of doing a reduce /**
* Combine the tokens in map2 with the tokens on map1, where tokens on map1 take precedence
* @param map1 the base token map
* @param map2 the map of additioanl tokens to add to the base map
*/
export function combineMaps(map1: TokenAddressMap, map2: TokenAddressMap): TokenAddressMap { export function combineMaps(map1: TokenAddressMap, map2: TokenAddressMap): TokenAddressMap {
const chainIds = Object.keys({ ...map1, ...map2 }).map((id) => parseInt(id)) const chainIds = Object.keys(
return chainIds.reduce( Object.keys(map1)
(acc, chainId) => ({ .concat(Object.keys(map2))
...acc, .reduce<{ [chainId: string]: true }>((memo, value) => {
[chainId]: { ...map2[chainId], ...map1[chainId] }, memo[value] = true
}), return memo
{} }, {})
) ).map((id) => parseInt(id))
return chainIds.reduce<Mutable<TokenAddressMap>>((memo, chainId) => {
memo[chainId] = {
...map2[chainId],
// map1 takes precedence
...map1[chainId],
}
return memo
}, {}) as TokenAddressMap
} }
// merge tokens contained within lists from urls // merge tokens contained within lists from urls