当前位置:网站首页>记忆函数的性能优化
记忆函数的性能优化
2022-07-02 09:46:00 【1900's 88 keys】
求阶乘
一般求阶乘的方法
function factorial(n) {
if (n == 0 || n == 1) return 1;
return n * factorial(n - 1)
}
console.time("factorial");
factorial(3000)
console.timeEnd("factorial");

可以看到直接运行时间大概在0.3s左右
通过hash表来进行缓存
let cache = {
};
function getCache(n) {
if (cache[n]) return cache[n];
if (n == 0 || n == 1) return (cache[n] = 1);
return (cache[n] = n * getCache(n - 1));
}
console.time("getCache-first");
getCache(3000)
console.timeEnd("getCache-first");
console.time("getCache-second");
getCache(2000)
console.timeEnd("getCache-second");

可以发现第一次调用时0.9s 第二次调用就是0.01s
高阶记忆函数
将该过程进行抽象化成一个函数让其拥有通用性
function memorize(fn) {
let cache = {
};
return function () {
let key = fn.name + '_' + [].join.call(arguments, ','); //函数名和参数进行拼接
// console.log(key, arguments);
return (cache[key] = cache[key] || fn.apply(this, arguments));
}
}
let memorizeFactory = memorize(factorial);
console.time("memorizeFactory-first");
memorizeFactory(3000)
console.timeEnd("memorizeFactory-first");
console.time("memorizeFactory-second");
memorizeFactory(3000)
console.timeEnd("memorizeFactory-second");

实现缓存
import {
api} form "./utils";
const cache = {
};
const request = async (url) => {
if(cache[url]){
return cache[url]
}
const response = await api(url);
cache[url] = response;
return response
}
边栏推荐
- Browser node event loop
- 传感器 ADXL335BCPZ-RL7 3轴 加速度计 符合 RoHS/WEEE
- 阿里初面被两道编程题给干掉,再次内推终上岸(已拿电子offer)
- Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?
- js1day(输入输出语法,数据类型,数据类型转换,var和let区别)
- [opencv learning] [moving object detection]
- Js10day (API phased completion, regular expression introduction, custom attributes, filtering sensitive word cases, registration module verification cases)
- Obtain file copyright information
- Js5day (event monitoring, function assignment to variables, callback function, environment object this, select all, invert selection cases, tab column cases)
- 腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
猜你喜欢

Unity SKFramework框架(二十一)、Texture Filter 贴图资源筛选工具

Js8day (rolling event (scroll family), offset family, client family, carousel map case (to be done))

Js4day (DOM start: get DOM element content, modify element style, modify form element attributes, setinterval timer, carousel Map Case)

Unity SKFramework框架(十七)、FreeCameraController 上帝视角/自由视角相机控制脚本
![[opencv learning] [contour detection]](/img/96/aaec61f137e4526c2c329e6fcfa1a2.jpg)
[opencv learning] [contour detection]

Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?

Linear DP acwing 897 Longest common subsequence

Js7day (event object, event flow, event capture and bubble, prevent event flow, event delegation, student information table cases)

Finally, someone explained the supervised learning clearly
![[200 opencv routines] 100 Adaptive local noise reduction filter](/img/89/9e9b667dd28cb25af005b6028ef26c.jpg)
[200 opencv routines] 100 Adaptive local noise reduction filter
随机推荐
Interesting interview questions
Fundamentals of face recognition (facenet)
国产免费数据仓库ETL调度自动化运维专家—TASKCTL
Ntmfs4c05nt1g N-ch 30V 11.9a MOS tube, pdf
Should I have a separate interface assembly- Should I have a separate assembly for interfaces?
About wechat enterprise payment to change x509certificate2 read certificate information, publish to the server can not access the solution
Std:: vector batch import fast de duplication method
模数转换器(ADC) ADE7913ARIZ 专为三相电能计量应用而设计
. Net wechat message template push
(7) Web security | penetration testing | how does network security determine whether CND exists, and how to bypass CND to find the real IP
Interval DP acwing 282 Stone merging
[opencv learning] [moving object detection]
3 A VTT端接 稳压器 NCP51200MNTXG资料
Oracle从入门到精通(第4版)
Get started REPORT | today, talk about the microservice architecture currently used by Tencent
Unity SKFramework框架(十二)、Score 计分模块
Obtain file copyright information
Js1day (syntaxe d'entrée / sortie, type de données, conversion de type de données, Var et let différenciés)
[200 opencv routines] 100 Adaptive local noise reduction filter
net share