typedash
Version:
modern, type-safe collection of utility functions
21 lines (20 loc) • 700 B
JavaScript
//#region src/functions/memoize/memoize.ts
/**
* Memoizes a function.
* @param fn The function to memoize.
* @param cacheKeyResolver An optional function used to resolve the cache key. Defaults to the first argument in the function call.
* @returns The memoized function.
*/
function memoize(fn, cacheKeyResolver) {
const cache = /* @__PURE__ */ new Map();
return function memoizedFunction(...args) {
const cacheKey = cacheKeyResolver ? cacheKeyResolver(...args) : args[0];
if (cache.has(cacheKey)) return cache.get(cacheKey);
const result = fn(...args);
cache.set(cacheKey, result);
return result;
};
}
//#endregion
export { memoize as t };
//# sourceMappingURL=memoize-BbSbrvR6.js.map