Open qappleh opened 3 years ago
function memoize(fn) {
return new Proxy(fn, {
cache: new Map(),
apply(target, thisArg, argsList) {
let cacheKey = argsList.toString();
if (!this.cache.has(cacheKey)) {
this.cache.set(cacheKey, target.apply(thisArg, argsList));
}
return this.cache.get(cacheKey);
}
});
}
const fibonacci = n => (n <= 2 ? 1 : fibonacci(n-1) + fibonacci(n-2));
const memoFibonacci = memoize(fibonacci);
注:斐波那契数列是指第N项为0,第一项为1,第N项(N>1)= 第N-2项 + 第N-1项的数列