forked from TheAlgorithms/JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Memoize.js
58 lines (51 loc) · 2 KB
/
Memoize.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
* @function memoize
* @description ->
* From [Wikipedia](https://en.wikipedia.org/wiki/Memoization),
* memoization is an optimization technique
* used primarily to speed up computer programs,
* by storing the results of expensive function calls
* and returning the cached result when the same inputs occur again
* This function is a first class objects,
* which lets us use it as [Higher-Order Function](https://eloquentjavascript.net/05_higher_order.html)
* and return another function
* @param {Function} func Original function
* @param {Map} cache - it's receive any cache DS which have get, set & has method
* @returns {Function} Memoized function
*/
const memoize = (func, cache = new Map()) => {
const jsonReplacer = (_, value) => {
if (value instanceof Set) { // if the value is Set it's converted to Array cause JSON.stringify can't convert Set
return [...value]
}
if (value instanceof Map) { // if the value is Map it's converted to Object cause JSON.stringify can't convert Map
return Object.fromEntries(value)
}
return value
}
return (...args) => {
/**
* Arguments converted to JSON string for use as a key of Map - it's easy to detect collections like -> Object and Array
* If the args input is -> [new Set([1, 2, 3, 4]), {name: 'myName', age: 23}]
* Then the agrsKey generate to -> '[[1,2,3,4],{"name":"myName","age":23}]' which is JSON mean string
* Now it's ready to be a perfect key for Map
*/
const argsKey = JSON.stringify(args, jsonReplacer)
/**
* Checks if the argument is already present in the cache,
* then return the associated value / result
*/
if (cache.has(argsKey)) {
return cache.get(argsKey)
}
/**
* If the argument is not yet present in the cache,
* execute original function and save its value / result in cache,
* finally return it
*/
const result = func(...args) // spread all args
cache.set(argsKey, result)
return result
}
}
export { memoize }