SOURCE

function memoize(f) {
    const cache = new Map();
    return function (...args) {
        let key = args.length + args.join('+');
        console.log('print key:', key);
        if (cache.has(key)) {
            console.log('cache:',key);
            return cache.get(key);
        } else {
            let result = f.apply(this, args);
            cache.set(key, result);
            return result;
        }

    }
}

function gcd(a, b) {
    if (a < b) {
        [a, b] = [b, a];
    }
    while (b !== 0) {
        [a, b] = [b, a % b];
    }
    return a;
}

const gcdmemo = memoize(gcd);
console.log('print:', gcdmemo(85, 187));

const factorial = memoize(function (n) {
    return (n <= 1) ? 1 : n * factorial(n - 1);
})
console.log('print:',factorial(5));
console 命令行工具 X clear

                    
>
console