typedash
Version:
modern, type-safe collection of utility functions
27 lines (25 loc) • 790 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
Object.defineProperty(exports, 'memoize', {
enumerable: true,
get: function () {
return memoize;
}
});
//# sourceMappingURL=memoize-tqOVguju.cjs.map