() } } 添加计数引用 在学习其他库 memoizee[4] 的过程中,我看到了如下用法: memoized = memoize(fn, { refCounter: true }); memoized (cacheKey) } return currentCache.get(cacheKey) as V; } }) as any } 完整代码在 memoizee\ return new Function('return '+ fn.toString())(); } 参考资料 前端 api 请求缓存方案[8] ECMAScript 6 入门 代理篇[9] memoizee : https://github.com/medikoo/memoizee [5] memoizee-proxy: https://github.com/wsafight/memoizee-proxy : https://github.com/medikoo/memoizee [11] memoizee-proxy: https://github.com/wsafight/memoizee-proxy
幸运的是,memoizee(https://www.npmjs.com/package/memoizee) 库支持此功能。 我们的最后一个示例变为: import memoize from 'memoizee'; const getUserById = memoize(async (userId: string): Promise
How to memoize calculations[13] 缓存优化往往是最简单有效的优化方式,但 useMemo 缓存加速只能缓存最近一次函数执行的结果,如果想缓存更多次函数执行的结果,可使用 memoizee How to memoize calculations: https://reactjs.org/docs/hooks-faq.html#how-to-memoize-calculations [14] memoizee : https://www.npmjs.com/package/memoizee [15] React 官方推荐: https://reactjs.org/docs/lists-and-keys.html