UNPKG

nextdevkit

Version:

A Comprehensive CLI Toolkit for Next.js Development

27 lines (26 loc) 862 B
const memoize = (fn, options = {}) => { const { maxSize = 1000, serializer = (...args) => JSON.stringify(args) } = options; const cache = new Map(); const memoizedFn = ((...args) => { const key = serializer(...args); if (cache.has(key)) { return cache.get(key); } const result = fn(...args); cache.set(key, result); if (cache.size > maxSize) { // Remove the oldest entry const iterator = cache.keys(); const firstKey = iterator.next().value; if (typeof firstKey === 'string') { cache.delete(firstKey); } else { console.warn('Attempted to delete an undefined key from the cache.'); } } return result; }); return memoizedFn; }; export default memoize;