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

Function memoize

src/helpers/get.js:713–734  ·  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

711 * _.memoize.Cache = WeakMap;
712 */
713function memoize(func, resolver) {
714 if (
715 typeof func != 'function' ||
716 (resolver && typeof resolver != 'function')
717 ) {
718 throw new TypeError(FUNC_ERROR_TEXT);
719 }
720 var memoized = function() {
721 const args = arguments,
722 key = resolver ? resolver.apply(this, args) : args[0],
723 cache = memoized.cache;
724
725 if (cache.has(key)) {
726 return cache.get(key);
727 }
728 const result = func.apply(this, args);
729 memoized.cache = cache.set(key, result);
730 return result;
731 };
732 memoized.cache = new (memoize.Cache || MapCache)();
733 return memoized;
734}
735
736// Assign cache to `_.memoize`.
737memoize.Cache = MapCache;

Callers 1

get.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…