当前位置:网站首页>什么是memoization,它有什么用?
什么是memoization,它有什么用?
2022-08-03 22:22:00 【紫微前端】
memoization是一个构建函数的过程,该函数能够记住它以前计算的结果或值。
制作记忆函数的用途是,如果该函数已在上次计算中使用相同的参数执行,我们将避免计算该函数。这节省了时间,但也有一个缺点,即我们将消耗更多的内存来保存以前的结果。
实现一个memoization helper函数。
function memoize(fn) {
const cache = {};
return function (param) {
if (cache[param]) {
console.log('cached');
return cache[param];
} else {
let result = fn(param);
cache[param] = result;
console.log(`not cached`);
return result;
}
}
}
const toUpper = (str ="")=> str.toUpperCase();
const toUpperMemoized = memoize(toUpper);
toUpperMemoized("abcdef");
toUpperMemoized("abcdef");
这个memoize辅助函数仅适用于接受一个参数的函数。我们需要创建一个接受多个参数的memoize</
边栏推荐
- 优化查询(工作中)
- 授人以渔 - 如何自行查询任意 SAP UI5 控件属性的文档和技术实现细节试读版
- CAS:122567-66-2_DSPE-生物素_DSPE-Biotin
- Data_web(九)mongodb增量同步到mongodb
- 【云原生实用技巧】使用 skopeo 批量同步 helm chart 依赖镜像
- 藏宝计划TreasureProject(TPC)系统模式开发技术原理
- 深度学习和机器学习有什么区别?
- node连接mysql数据库报错:Client does not support authentication protocol requested by server
- PowerMockup 4.3.4::::Crack
- 易观分析:2022年Q2中国网络零售B2C市场交易规模达23444.7亿元
猜你喜欢
113. Teach a Man how to fish - How to query the documentation and technical implementation details of any SAP UI5 control property by yourself
嵌入式系统:时钟
《数字经济全景白皮书》金融数字用户篇 重磅发布!
LabVIEW代码生成错误 61056
Pay from 0 to 1
CAS: 773888-45-2_BIOTIN ALKYNE_Biotin-alkynyl
Summary bug 】 【 Elipse garbled solution project code in Chinese!
2019年10月SQL注入的两倍
DO280管理和监控OpenShift平台--资源限制
VLAN实验
随机推荐
HDU 5655 CA Loves Stick
『百日百题 · 基础篇』备战面试,坚持刷题 第四话——循环语句!
DO280管理和监控OpenShift平台--资源限制
从0到1看支付
noip preliminary round
Golang Chapter 1: Getting Started
东西向和南北向通信的统一
【day1】
IO thread process -> thread synchronization mutual exclusion mechanism -> day6
关于Yii2批量更新的操作
Golang Chapter 2: Program Structure
Lift, Splat, Shoot: Encoding Images from Arbitrary Camera Rigs by Implicitly Unprojecting to 3D 论文笔记
关于IDO预售系统开发技术讲解丨浅谈IDO预售合约系统开发原理分析
全球观之地理部分
.NET6之MiniAPI(十四):跨域CORS(上)
386. Lexicographical Numbers
Optimize the query (work in progress)
encapsulation, package, access modifier, static variable
for循环练习题
Data_web(九)mongodb增量同步到mongodb