From 8a06ac7fb9a8ee23530bcb385c06af917e2fdc55 Mon Sep 17 00:00:00 2001 From: Matchu Date: Tue, 24 Nov 2020 21:03:45 -0800 Subject: [PATCH] perf upgrade for UserItemsPage MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I knowingly wrote this less performant at first. And indeed, it was slow—like 2sec of main thread time! Turn that O(mn) into O(n) pls! --- .../__snapshots__/User.test.js.snap | 4 +-- src/server/types/User.js | 25 +++++++++++++------ 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/src/server/query-tests/__snapshots__/User.test.js.snap b/src/server/query-tests/__snapshots__/User.test.js.snap index b5c6116..a17d006 100644 --- a/src/server/query-tests/__snapshots__/User.test.js.snap +++ b/src/server/query-tests/__snapshots__/User.test.js.snap @@ -69,7 +69,7 @@ Object { "name": "Blue Jelly Tiara", }, ], - "name": "(Not in a list)", + "name": "Not in a list", "ownsOrWantsItems": "OWNS", }, Object { @@ -81,7 +81,7 @@ Object { "name": "Altador Cup Background - Kreludor", }, ], - "name": "(Not in a list)", + "name": "Not in a list", "ownsOrWantsItems": "WANTS", }, ], diff --git a/src/server/types/User.js b/src/server/types/User.js index 5c521a3..9372cda 100644 --- a/src/server/types/User.js +++ b/src/server/types/User.js @@ -136,12 +136,25 @@ const resolvers = { userLoader.load(id), ]); + const hangersByList = new Map(closetLists.map((l) => [l.id, []])); + const defaultListOwnedHangers = []; + const defaultListWantedHangers = []; + for (const hanger of allClosetHangers) { + if (hanger.listId) { + hangersByList.get(hanger.listId).push(hanger); + } else if (hanger.owned) { + defaultListOwnedHangers.push(hanger); + } else { + defaultListWantedHangers.push(hanger); + } + } + const closetListNodes = closetLists .filter((closetList) => isCurrentUser || closetList.visibility >= 1) .map((closetList) => ({ id: closetList.id, - items: allClosetHangers - .filter((h) => h.listId === closetList.id) + items: hangersByList + .get(closetList.id) .map((h) => ({ id: h.itemId })), })); @@ -150,9 +163,7 @@ const resolvers = { isDefaultList: true, userId: id, ownsOrWantsItems: "OWNS", - items: allClosetHangers - .filter((h) => h.listId == null && h.owned) - .map((h) => ({ id: h.itemId })), + items: defaultListOwnedHangers.map((h) => ({ id: h.itemId })), }); } @@ -162,9 +173,7 @@ const resolvers = { userId: id, ownsOrWantsItems: "WANTS", isDefaultList: true, - items: allClosetHangers - .filter((h) => h.listId == null && !h.owned) - .map((h) => ({ id: h.itemId })), + items: defaultListWantedHangers.map((h) => ({ id: h.itemId })), }); }