* 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)
| 711 | * _.memoize.Cache = WeakMap; |
| 712 | */ |
| 713 | function 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`. |
| 737 | memoize.Cache = MapCache; |
no outgoing calls
no test coverage detected
searching dependent graphs…