mirror of
https://gitlab.com/mfocko/LeetCode.git
synced 2024-11-14 09:59:41 +01:00
29 lines
483 B
JavaScript
29 lines
483 B
JavaScript
|
/**
|
||
|
* @param {Function} fn
|
||
|
*/
|
||
|
function memoize(fn) {
|
||
|
let cache = new Map();
|
||
|
|
||
|
return function(...args) {
|
||
|
let key = JSON.stringify(args);
|
||
|
|
||
|
if (!cache.has(key)) {
|
||
|
cache.set(key, fn(...args));
|
||
|
}
|
||
|
|
||
|
return cache.get(key);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
/**
|
||
|
* let callCount = 0;
|
||
|
* const memoizedFn = memoize(function (a, b) {
|
||
|
* callCount += 1;
|
||
|
* return a + b;
|
||
|
* })
|
||
|
* memoizedFn(2, 3) // 5
|
||
|
* memoizedFn(2, 3) // 5
|
||
|
* console.log(callCount) // 1
|
||
|
*/
|