Question 45 of 10045%
45
This memoize helper caches results by stringified arguments. What gets logged, in order?
function memoize(fn) {
const cache = new Map();
return function (...args) {
const key = JSON.stringify(args);
if (cache.has(key)) {
console.log("from cache");
return cache.get(key);
}
const result = fn(...args);
cache.set(key, result);
return result;
};
}
function slowSquare(n) {
return n * n;
}
const memoSquare = memoize(slowSquare);
console.log(memoSquare(4));
console.log(memoSquare(4));
console.log(memoSquare(5));
45/100