[Javascript] Memoization
2021-03-28 18:26
标签:null script ring javascrip cto mem color return turn [Javascript] Memoization 标签:null script ring javascrip cto mem color return turn 原文地址:https://www.cnblogs.com/Answer1215/p/13622386.htmlconst factorial = (n) => (n > 1 ? n * factorial(n - 1) : 1);
const memoize = (fn) => {
const cache = {};
return (...args) => {
const key = JSON.stringify(args);
return key in cache ? cache[key] : (cache[key] = fn.apply(null, args));
};
};
const memoizedFactorial = memoize(factorial);
console.log(memoizedFactorial(9, { name: ‘Todd Motto‘ }));
console.log(memoizedFactorial(6));
console.log(memoizedFactorial(9, { name: ‘Todd Motto‘ }));
下一篇:Java对象克隆
文章标题:[Javascript] Memoization
文章链接:http://soscw.com/index.php/essay/69165.html