-
-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy path8-timerMemoize.js
More file actions
35 lines (32 loc) · 898 Bytes
/
8-timerMemoize.js
File metadata and controls
35 lines (32 loc) · 898 Bytes
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
'use strict';
const argKey = x => (x.toString() + ':' + typeof(x));
const generateKey = arg => arg.map(argKey).join('|');
const timerMemoize = (fn, msec) => {
const cache = new Map();
return (...args) => {
const key = generateKey(args);
const timeout = setTimeout(() => cache.delete(key), msec);
if (cache.has(key)) {
const value = cache.get(key);
console.log('From cache: ', value.res);
clearTimeout(value.timeout);
value.timeout = timeout;
return value.res;
}
const res = fn(...args);
cache.set(key, { timeout, res });
console.log('Calculated: ', res);
return res;
};
};
const sum = (a, b) => a + b;
const f1 = timerMemoize(sum, 10000);
f1(1, 2);
f1(1, 2);
f1(1, 2);
f1(2, 4);
f1(2, 4);
setTimeout(() => f1(2, 4), 9000);
setTimeout(() => f1(2, 4), 12000);
setTimeout(() => f1(2, 4), 22500);
setTimeout(() => f1(2, 4), 23000);