(func, cache = new Map())
| 14 | * @returns {Function} Memoized function |
| 15 | */ |
| 16 | const memoize = (func, cache = new Map()) => { |
| 17 | const jsonReplacer = (_, value) => { |
| 18 | if (value instanceof Set) { |
| 19 | // if the value is Set it's converted to Array cause JSON.stringify can't convert Set |
| 20 | return [...value] |
| 21 | } |
| 22 | |
| 23 | if (value instanceof Map) { |
| 24 | // if the value is Map it's converted to Object cause JSON.stringify can't convert Map |
| 25 | return Object.fromEntries(value) |
| 26 | } |
| 27 | |
| 28 | return value |
| 29 | } |
| 30 | |
| 31 | return (...args) => { |
| 32 | /** |
| 33 | * Arguments converted to JSON string for use as a key of Map - it's easy to detect collections like -> Object and Array |
| 34 | * If the args input is -> [new Set([1, 2, 3, 4]), {name: 'myName', age: 23}] |
| 35 | * Then the argsKey generate to -> '[[1,2,3,4],{"name":"myName","age":23}]' which is JSON mean string |
| 36 | * Now it's ready to be a perfect key for Map |
| 37 | */ |
| 38 | const argsKey = JSON.stringify(args, jsonReplacer) |
| 39 | |
| 40 | /** |
| 41 | * Checks if the argument is already present in the cache, |
| 42 | * then return the associated value / result |
| 43 | */ |
| 44 | if (cache.has(argsKey)) { |
| 45 | return cache.get(argsKey) |
| 46 | } |
| 47 | |
| 48 | /** |
| 49 | * If the argument is not yet present in the cache, |
| 50 | * execute original function and save its value / result in cache, |
| 51 | * finally return it |
| 52 | */ |
| 53 | const result = func(...args) // spread all args |
| 54 | cache.set(argsKey, result) |
| 55 | |
| 56 | return result |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | export { memoize } |
no test coverage detected