UNPKG

promptdesk

Version:
31 lines (30 loc) 1.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.memoize = void 0; 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]; }; }; exports.memoize = memoize;