MCPcopy Create free account
hub / github.com/Lemoncode/fonk / memoize

Function memoize

src/helpers/set.js:778–799  ·  view source on GitHub ↗

* Creates a function that memoizes the result of `func`. If `resolver` is * provided, it determines the cache key for storing the result based on the * arguments provided to the memoized function. By default, the first argument * provided to the memoized function is used as the map cache key. The

(func, resolver)

Source from the content-addressed store, hash-verified

776 * _.memoize.Cache = WeakMap;
777 */
778function memoize(func, resolver) {
779 if (
780 typeof func != 'function' ||
781 (resolver && typeof resolver != 'function')
782 ) {
783 throw new TypeError(FUNC_ERROR_TEXT);
784 }
785 var memoized = function () {
786 var args = arguments,
787 key = resolver ? resolver.apply(this, args) : args[0],
788 cache = memoized.cache;
789
790 if (cache.has(key)) {
791 return cache.get(key);
792 }
793 var result = func.apply(this, args);
794 memoized.cache = cache.set(key, result);
795 return result;
796 };
797 memoized.cache = new (memoize.Cache || MapCache)();
798 return memoized;
799}
800
801// Assign cache to `_.memoize`.
802memoize.Cache = MapCache;

Callers 1

set.jsFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…