当前位置:网站首页>What is memoization and what is it good for?
What is memoization and what is it good for?
2022-08-03 22:27:00 【Ziwei front end】
memoization is the process of building a function that remembers the results or values of its previous calculations.
The purpose of making a memorized function is that we will avoid evaluating the function if it has already been executed with the same parameters in the last evaluation.This saves time, but also has the disadvantage that we will consume more memory to save previous results.
Implement a memoization helper function.
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");
This memoize helper function only works with functions that accept one argument.We need to create a memoize
that accepts multiple parameters边栏推荐
- Golang Chapter 2: Program Structure
- Recognized by International Authorities | Yunzhuang Technology was selected in "RPA Global Market Pattern Report, Q3 2022"
- 为什么我们需要回调
- 386. Lexicographical Numbers
- Zilliz 2023 秋季校园招聘正式启动!
- Codeup刷题笔记-简单模拟
- October 2019 Twice SQL Injection
- MiniAPI of .NET6 (14): Cross-domain CORS (Part 1)
- Teach a Man How to Fish - How to Query the Properties of Any SAP UI5 Control by Yourself Documentation and Technical Implementation Details Demo
- pikachu Over permission
猜你喜欢
斩获双奖|易知微荣获“2021中国数字孪生解决方案优秀供应商”“中国智能制造优秀推荐产品”双奖项!
嵌入式系统:时钟
Cisco ike2 IPSec configuration
MiniAPI of .NET6 (14): Cross-domain CORS (Part 1)
藏宝计划TreasureProject(TPC)系统模式开发技术原理
pikachu Over permission 越权
中国企业构建边缘计算解决方案的最佳实践
Diazo Biotin-PEG3-DBCO | Diazo Compound Modified Biotin-Tripolyethylene Glycol-Dibenzocyclooctyne
封装、包、访问权限修饰符、static变量
《数字经济全景白皮书》金融数字用户篇 重磅发布!
随机推荐
如何基于WPF写一款数据库文档管理工具(二)
Flutter 桌面探索 | 自定义可拖拽导航栏
21天打卡挑战学习MySQL—Day第一周 第一篇
Data_web(九)mongodb增量同步到mongodb
Why do we need callbacks
如何创建一个Web项目
472. Concatenated Words
E-commerce data warehouse ODS layer-----log data loading
嵌入式开发:嵌入式基础——代码和数据空间揭秘
CAS:1620523-64-9_Azide-SS-biotin_biotin-disulfide-azide
UVa 1025 - A Spy in the Metro (White Book)
Pay from 0 to 1
HDU 5655 CA Loves Stick
pikachu Over permission
Embedded Systems: GPIO
Cisco ike2 IPSec configuration
关于IDO预售系统开发技术讲解丨浅谈IDO预售合约系统开发原理分析
【云原生实用技巧】使用 skopeo 批量同步 helm chart 依赖镜像
《数字经济全景白皮书》金融数字用户篇 重磅发布!
七夕快乐!