From d05e25ef72f390d76d67a88b0d678d1ebbc109db Mon Sep 17 00:00:00 2001 From: Patrick Hulin Date: Sat, 12 Oct 2024 12:44:00 -0400 Subject: [PATCH 1/2] perf: Memoize getFoldGroup. --- src/lib.ts | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/src/lib.ts b/src/lib.ts index 5bc3884909..3a68f3ba04 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -455,12 +455,23 @@ export function isCurrentFamiliar(familiar: Familiar): boolean { * * @category General * @param item Item that is part of the required fold group + * @param cache Whether to query the fold group cache * @returns List of items in the fold group */ -export function getFoldGroup(item: Item): Item[] { - return Object.entries(getRelated(item, "fold")) +const foldGroupCache = new Map(); +export function getFoldGroup(item: Item, cache = true): Item[] { + if (cache) { + const cached = foldGroupCache.get(item); + if (cached !== undefined) return cached; + } + + const result = Object.entries(getRelated(item, "fold")) .sort(([, a], [, b]) => a - b) .map(([i]) => Item.get(i)); + for (const fold of result) { + foldGroupCache.set(fold, result); + } + return result; } /** From 97de4ab64f1a765ac6908847dca872b7b8772d0e Mon Sep 17 00:00:00 2001 From: Patrick Hulin Date: Sat, 12 Oct 2024 12:48:47 -0400 Subject: [PATCH 2/2] lint --- src/lib.ts | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/lib.ts b/src/lib.ts index 3a68f3ba04..0902448d10 100644 --- a/src/lib.ts +++ b/src/lib.ts @@ -450,6 +450,8 @@ export function isCurrentFamiliar(familiar: Familiar): boolean { return myFamiliar() === familiar; } +const foldGroupCache = new Map(); + /** * Determines the fold group (if any) of which the given item is a part * @@ -458,7 +460,6 @@ export function isCurrentFamiliar(familiar: Familiar): boolean { * @param cache Whether to query the fold group cache * @returns List of items in the fold group */ -const foldGroupCache = new Map(); export function getFoldGroup(item: Item, cache = true): Item[] { if (cache) { const cached = foldGroupCache.get(item);