面试官:手写一个记忆函数memoize叭。

428 阅读1分钟

函数记忆是指将之前计算的结果缓存起来,以后调用时。如果遇到相同的参数,就直接返回缓存中的数据,是一种经典的空间换时间的方法。

代码

let memoize = function (fn) {
    let temp = { };
    return function (...args) {
        const key = JSON.stringify(args);
        temp[key] = temp[key] || fn.call(fn, ...args)
        return temp[key];
    }
}

测试

let add = function (a, b) {
    console.log(a, b);
    return a + b;
}

let foo = memoize(add)

foo(1, 2)
foo(1, 2)
foo(4, 3)
foo(4, 2)

image.png


记录记录!