perf upgrade for UserItemsPage

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!
This commit is contained in:
Emi Matchu 2020-11-24 21:03:45 -08:00
parent a7e32232e2
commit 8a06ac7fb9
2 changed files with 19 additions and 10 deletions

View file

@ -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",
},
],

View file

@ -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 })),
});
}