memoize

 

function getArea(r){
    console.log(r);
    return Math.PI * r * r
}

function memoize(f){
    let cache = {};
    return function(){
        let key = JSON.stringify(arguments);
        cache[key] = cache[key] || f.apply(f,arguments);
        console.log('key',key,cache,)
        return cache[key]
    }
}
let getMemoize = memoize(getArea);
getMemoize(5);
getMemoize(6);
getMemoize(7);
getMemoize(5);
getMemoize(6);
getMemoize(7);

 

posted @ 2021-09-18 17:10  LaLaLa_heng  阅读(68)  评论(0编辑  收藏  举报