UNPKG

promptdesk

Version:
27 lines (26 loc) 998 B
export const memoize = (fn, limit = 100) => { let cache = {}; let keys = []; // This will track the "use" order for LRU eviction return (...args) => { const key = JSON.stringify(args); const found = key in cache; if (found) { // If the key is found, move it to the end to mark it as recently used keys = keys.filter(k => k !== key).concat(key); } else { const result = fn(...args); cache[key] = result; keys.push(key); // Add the new key to the end (most recently used position) // If the cache exceeds the limit, remove the least recently used item if (keys.length > limit) { const oldestKey = keys.shift(); // This is the least recently used key if (oldestKey !== undefined) { delete cache[oldestKey]; } } return result; } return cache[key]; }; };