From 3686506f2a6dc3eb83c51b4f611cc3d93cd466d5 Mon Sep 17 00:00:00 2001 From: Moody Salem Date: Fri, 23 Jul 2021 09:27:23 -0500 Subject: [PATCH] perf: remove extra spread operators from the combine maps function --- src/state/lists/hooks.ts | 31 ++++++++++++++++++++++--------- 1 file changed, 22 insertions(+), 9 deletions(-) diff --git a/src/state/lists/hooks.ts b/src/state/lists/hooks.ts index 1243835c4f..c75530c0e1 100644 --- a/src/state/lists/hooks.ts +++ b/src/state/lists/hooks.ts @@ -47,16 +47,29 @@ export function useAllLists(): AppState['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 { - const chainIds = Object.keys({ ...map1, ...map2 }).map((id) => parseInt(id)) - return chainIds.reduce( - (acc, chainId) => ({ - ...acc, - [chainId]: { ...map2[chainId], ...map1[chainId] }, - }), - {} - ) + const chainIds = Object.keys( + Object.keys(map1) + .concat(Object.keys(map2)) + .reduce<{ [chainId: string]: true }>((memo, value) => { + memo[value] = true + return memo + }, {}) + ).map((id) => parseInt(id)) + + return chainIds.reduce>((memo, chainId) => { + memo[chainId] = { + ...map2[chainId], + // map1 takes precedence + ...map1[chainId], + } + return memo + }, {}) as TokenAddressMap } // merge tokens contained within lists from urls