blob: f66a155a34bf64ffa4f9697ca46086938d14b2b9 (
plain)
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
|
export default function (callback, options = {}) {
let { bench, name } = options;
let cache = new Map();
return (...args) => {
// Only supports single-arg functions for now.
if (args.filter(Boolean).length > 1) {
bench?.get(`(count) ${name} Not valid for memoize`).incrementCount();
return callback(...args);
}
let [cacheKey] = args;
if (!cache.has(cacheKey)) {
cache.set(cacheKey, callback(...args));
bench?.get(`(count) ${name} memoize miss`).incrementCount();
return cache.get(cacheKey);
}
bench?.get(`(count) ${name} memoize hit`).incrementCount();
return cache.get(cacheKey);
};
}
|