当前位置:网站首页>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边栏推荐
- 【bug】汇总Elipse项目中代码中文乱码解决方法!
- 4年工作经验,多线程间的5种通信方式都说不出来,你敢信?
- noip初赛
- Research status of target detection at home and abroad
- Makefile
- With 4 years of work experience, the 5 communication methods between multi-threads can't be said, can you believe it?
- L2-041 插松枝
- golang写的存储引擎,基于b+树,mmap
- PowerMockup 4.3.4::::Crack
- 物联网新零售模式,引领购物新潮流
猜你喜欢
投资性大于游戏性 NFT游戏到底是不是门好生意
全球观之地理部分
Gains double award | know micro easily won the "2021 China digital twin solution suppliers in excellence" "made in China's smart excellent recommended products" double award!
for循环练习题
[b01lers2020]Life on Mars
Flutter 桌面探索 | 自定义可拖拽导航栏
网络基础学习系列四(网络层,数据链路层和一些其他重要协议或技术)
如何创建一个Web项目
CAS:153162-70-0_N-BOC-6-Biotinamidohexylamine
2022-08-02 mysql/stonedb slow SQL-Q18 - memory usage surge analysis
随机推荐
Quickly build a website with static files
386. Lexicographical Numbers
关于Yii2批量更新的操作
JPA Native Query(本地查询)及查询结果转换
noip初赛
Makefile
for循环练习题
HCIP第十六天
mysql如何将表结构导出到excel
【day6】类与对象、封装、构造方法
Kubernetes入门到精通-Operator 模式
HCIP BGP实验报告
【开源框架】国内首个通用云计算框架,任意程序都可做成云计算。
目标检测的国内外研究现状
384. Shuffle an Array
Codeup brushing notes - simple simulation
静态文件快速建站
深度学习和机器学习有什么区别?
如何创建一个Web项目
嵌入式系统:时钟