You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constmemoizAddition=()=>{letcache={};return(value)=>{if(valueincache){console.log("Fetching from cache");returncache[value];// Here, cache.value cannot be used as property name starts with the number which is not a valid JavaScript identifier. Hence, can only be accessed using the square bracket notation.}else{console.log("Calculating result");letresult=value+20;cache[value]=result;returnresult;}};};// returned function from memoizAdditionconstaddition=memoizAddition();console.log(addition(20));//output: 40 calculatedconsole.log(addition(20));//output: 40 cached
The text was updated successfully, but these errors were encountered:
记忆化是一种编程技术,它试图通过缓存以前计算的结果来提高函数的性能。每次调用 memoized 函数时,它的参数都用于索引缓存。如果数据存在,则可以将其返回,而无需执行整个函数。否则执行该函数,然后将结果添加到缓存中。让我们举一个使用 memoization 添加函数的例子
The text was updated successfully, but these errors were encountered: