当前位置:网站首页>什么是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</
边栏推荐
- 2022的七夕,奉上7个精美的表白代码,同时教大家快速改源码自用
- 113. 授人以渔 - 如何自行查询任意 SAP UI5 控件属性的文档和技术实现细节
- win10系统下yolov5-V6.1版本的tensorrt部署细节教程及bug修改
- CAS:122567-66-2_DSPE-Biotin_DSPE-Biotin
- LabVIEW代码生成错误 61056
- CAS: 773888-45-2_BIOTIN ALKYNE_Biotin-alkynyl
- UVa 1025 - A Spy in the Metro (White Book)
- 466. Count The Repetitions
- Cisco ike2 IPSec配置
- 函数,递归以及dom简单操作
猜你喜欢

全球观之地理部分

超级实用网站+公众号合集

老板:公司系统太多,能不能实现账号互通?

CAS:1620523-64-9_Azide-SS-biotin_生物素-二硫-叠氮

Lift, Splat, Shoot: Encoding Images from Arbitrary Camera Rigs by Implicitly Unprojecting to 3D 论文笔记

2022-08-03 Oracle executes slow SQL-Q17 comparison

HCIP第十六天

嵌入式开发:嵌入式基础——代码和数据空间揭秘

Embedded systems: overview

CAS:908007-17-0_Biotin-azide_Biotin azide
随机推荐
navicat 连接 mongodb 报错[13][Unauthorized] command listDatabases requires authentication
CAS:1797415-74-7_TAMRA-Azide-PEG-Biotin
Makefile
网络基础学习系列四(网络层,数据链路层和一些其他重要协议或技术)
385. Mini Parser
October 2019 Twice SQL Injection
【刷题篇】二叉树的右视图
图的基础概念
Lift, Splat, Shoot: Encoding Images from Arbitrary Camera Rigs by Implicitly Unprojecting to 3D 论文笔记
472. Concatenated Words
LabVIEW代码生成错误 61056
Golang Chapter 1: Getting Started
Embedded systems: overview
『百日百题 · 基础篇』备战面试,坚持刷题 第四话——循环语句!
Data_web(八)mysql增量同步到mongodb
Summary bug 】 【 Elipse garbled solution project code in Chinese!
投资性大于游戏性 NFT游戏到底是不是门好生意
CAS:1260586-88-6_Biotin-C5-Azide_Biotin-C5-Azide
超级实用网站+公众号合集
互联网用户账号信息管理规定今起施行:必须严打账号买卖灰产