import { setDebug } from "./debug"; import { LruMap } from "./lru"; const cache = new LruMap(50); setDebug("cache", cache); function hashKeyParts(keyParts: string[]): string { const str = keyParts.join(":"); let hash = 5381; for (let i = 0; i < str.length; i++) { hash = hash * 33 ^ str.charCodeAt(i); } // Convert to unsigned 32-bit and base36 return (hash >>> 0).toString(36); } export function memo(keyParts: string[], factory: () => T): T { const key = hashKeyParts(keyParts); if (!cache.has(key)) { cache.set(key, factory()); } return cache.get(key) as T; }