Skip to content

Commit

Permalink
Merge pull request #669 from phulin/foldgroup
Browse files Browse the repository at this point in the history
perf: Memoize getFoldGroup.
  • Loading branch information
horrible-little-slime authored Oct 12, 2024
2 parents 9521528 + 97de4ab commit 77a12ff
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/lib.ts
Original file line number Diff line number Diff line change
Expand Up @@ -450,17 +450,29 @@ export function isCurrentFamiliar(familiar: Familiar): boolean {
return myFamiliar() === familiar;
}

const foldGroupCache = new Map<Item, Item[]>();

/**
* Determines the fold group (if any) of which the given item is a part
*
* @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"))
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;
}

/**
Expand Down

0 comments on commit 77a12ff

Please sign in to comment.