-
Notifications
You must be signed in to change notification settings - Fork 0
/
memoOne.js
73 lines (52 loc) · 1.89 KB
/
memoOne.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
https://bigfrontend.dev/problem/implement-memoizeOne
122. implement memoizeOne()
In problem 14. Implement a general memoization function, you are asked to implement a memo function without space concern.
But in reality, it could be a problem if cache bloats.
You might need to restrict the cache capacity, just like memoize-one , it only remembers the latest arguments and result.
Please implement your own memoizeOne(), it takes 2 arguments
target function
(optional) a equality check function to compare current and last arguments
Default equality check function should be a shallow comparison on array items with strict equal ===.
*/
/**
* @param {Function} func
* @param {(args: any[], newArgs: any[]) => boolean} [isEqual]
* @returns {any}
*/
function memoizeOne(func, isEqualFunc) {
const cache = {
_this: undefined,
key: undefined,
value: undefined,
}
return function(...args) {
let tempIsEqualFunc = isEqual;
if (typeof isEqualFunc === "function") if (cache.key !== undefined) tempIsEqualFunc = isEqualFunc;
if (tempIsEqualFunc(cache.key, args, cache._this, this)) return cache.value;
cache.value = func.call(this, ...args);
cache.key = args;
cache._this = this;
return cache.value;
}
function isEqual(cacheKey, args, cacheThis, givenThis) {
if (!cacheThis) return false;
if (!cacheKey) return false;
if (cacheThis !== givenThis) return false;
if (cacheKey.length !== args.length) return false;
for (let i = 0; i < args.length; i++) {
if (args[i] !== cacheKey[i]) return false;
}
return true;
}
}
function add(a, b) {
return a + b;
}
const memoizedAdd = memoizeOne(add);
memoizedAdd(1, 2);
// add function: is called
// [new value returned: 3]
memoizedAdd(1, 2);
// add function: not called
// [cached result is returned: 3]