UNPKG

fast-typescript-memoize

Version:

Fast memoization decorator and other helpers with 1st class support for Promises.

36 lines 1.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.memoize2 = void 0; /** * A simple intrusive 1-slot cache memoization helper for 2 parameters * functions. It's useful when we have a very high chance of hit rate and is * faster (and more memory efficient) than a Map<TArg1, Map<TArg2, TResult>> * based approach since it doesn't create intermediate maps. * * This method works seamlessly for async functions too: the returned Promise is * eagerly memoized, so all the callers will subscribe to the same Promise. * * Returns the new memoized function with 2 arguments for the `tag`. */ function memoize2(obj, tag, func) { if (!obj.hasOwnProperty(tag)) { let arg1Cache; let arg2Cache; let resultCache; Object.defineProperty(obj, tag, { enumerable: false, writable: false, value: (arg1, arg2) => { if (arg1Cache !== arg1 || arg2Cache !== arg2) { arg1Cache = arg1; arg2Cache = arg2; resultCache = func(arg1, arg2); } return resultCache; }, }); } return obj[tag]; } exports.memoize2 = memoize2; //# sourceMappingURL=memoize2.js.map