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
exportfunctiondefaultMemoize(func,equalityCheck=defaultEqualityCheck){letlastArgs=nullletlastResult=null// we reference arguments instead of spreading them for performance reasonsreturnfunction(){if(!areArgumentsShallowlyEqual(equalityCheck,lastArgs,arguments)){// apply arguments instead of spreading for performance.lastResult=func.apply(null,arguments)}lastArgs=argumentsreturnlastResult}}
importmemoizeOnefrom'memoize-one';constcanThrow=name=>{console.log('called');if(name==='throw'){thrownewError(name);}return{ name };};constmemoized=memoizeOne(canThrow);console.log(memoized.call(undefined));console.log(memoized.call(undefined));
The text was updated successfully, but these errors were encountered:
reselect
reselect使用的缓存主要是利用闭包,将上一次传入函数的参数与这次调用时传入的参数进行比较(浅比较),如果发生了改变就执行结果函数,然后利用闭包记录该次传入的参数与函数执行结果值:
同时在该包里面使用了两次该缓存函数:
传入的参数
各个依赖
的执行结果核心代码结果如下:
memoize-one
该库原理与
reselect
基本一致,也是利用了闭包
,不同的是他除了判断传入参数不一样外,还判断了函数执行时,上下文(this)
的不同,同时它还强调了结果函数抛出异常的情况,核心代码如下:The text was updated successfully, but these errors were encountered: