当前位置:网站首页>记忆函数的性能优化
记忆函数的性能优化
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
}
边栏推荐
- Hash table acwing 841 String hash
- The redis development document released by Alibaba covers all redis operations
- 上海交大教授:何援军——包围盒(包容体/包围盒子)
- [opencv learning] [moving object detection]
- Js3day (array operation, JS bubble sort, function, debug window, scope and scope chain, anonymous function, object, Math object)
- Unity skframework framework (XIV), extension extension function
- Linear DP acwing 896 Longest ascending subsequence II
- Day4 operator, self increasing, self decreasing, logical operator, bit operation, binary conversion decimal, ternary operator, package mechanism, document comment
- Ali was killed by two programming problems at the beginning, pushed inward again, and finally landed (he has taken an electronic offer)
- Unity skframework framework (XXI), texture filter map resource filtering tool
猜你喜欢

Js10day (API phased completion, regular expression introduction, custom attributes, filtering sensitive word cases, registration module verification cases)

Unity skframework framework (XVIII), roamcameracontroller roaming perspective camera control script

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

Unity SKFramework框架(十三)、Question 问题模块

C#修饰符

(7) Web security | penetration testing | how does network security determine whether CND exists, and how to bypass CND to find the real IP
![Jerry's watch stops ringing [article]](/img/28/8a225b77b19360d2b0077a2b8c4b6d.jpg)
Jerry's watch stops ringing [article]

挥发性有机物TVOC、VOC、VOCS气体检测+解决方案

Unity SKFramework框架(十九)、POI 兴趣点/信息点

Direct control PTZ PTZ PTZ PTZ camera debugging (c)
随机推荐
[opencv learning] [Canny edge detection]
国产免费数据仓库ETL调度自动化运维专家—TASKCTL
[opencv learning] [template matching]
Unity skframework framework (XXI), texture filter map resource filtering tool
Tencent three sides: in the process of writing files, the process crashes, and will the file data be lost?
3 A VTT端接 稳压器 NCP51200MNTXG资料
上手报告|今天聊聊腾讯目前在用的微服务架构
[opencv learning] [common image convolution kernel]
js5day(事件监听,函数赋值给变量,回调函数,环境对象this,全选反选案例,tab栏案例)
Jerry's watch modifies the alarm clock [chapter]
Unity SKFramework框架(十四)、Extension 扩展函数
Hash table acwing 840 Simulated hash table
应用LNK306GN-TL 转换器、非隔离电源
JS generates 4-digit verification code
Jerry's weather direction coding table [chapter]
Window10 upgrade encountered a big hole error code: 0xc000000e perfect solution
自主可控三维云CAD:CrownCAD赋能企业创新设计
C modifier
Day4 operator, self increasing, self decreasing, logical operator, bit operation, binary conversion decimal, ternary operator, package mechanism, document comment
PR usage skills, how to use PR to watermark?