当前位置:网站首页>记忆函数的性能优化
记忆函数的性能优化
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
}
边栏推荐
- Unity SKFramework框架(二十一)、Texture Filter 贴图资源筛选工具
- Fundamentals of face recognition (facenet)
- 阿里初面被两道编程题给干掉,再次内推终上岸(已拿电子offer)
- Oracle从入门到精通(第4版)
- C#运算符
- Ali was killed by two programming problems at the beginning, pushed inward again, and finally landed (he has taken an electronic offer)
- 8A Synchronous Step-Down regulator tps568230rjer_ Specification information
- How can attribute mapping of entity classes be without it?
- 获取文件版权信息
- 架构师必须了解的 5 种最佳软件架构模式
猜你喜欢
Linear DP acwing 897 Longest common subsequence
Unity skframework framework (XXI), texture filter map resource filtering tool
West digital decided to raise the price of flash memory products immediately after the factory was polluted by materials
Js10day (API phased completion, regular expression introduction, custom attributes, filtering sensitive word cases, registration module verification cases)
Browser node event loop
JS iterator generator asynchronous code processing promise+ generator - > await/async
Fundamentals of face recognition (facenet)
日本赌国运:Web3.0 ,反正也不是第一次失败了!
Jerry's watch gets the default ringtone selection list [article]
挥发性有机物TVOC、VOC、VOCS气体检测+解决方案
随机推荐
spfa AcWing 851. SPFA finding the shortest path
Structured data, semi-structured data and unstructured data
Unity skframework framework (XXI), texture filter map resource filtering tool
架构师必须了解的 5 种最佳软件架构模式
The redis development document released by Alibaba covers all redis operations
国产免费数据仓库ETL调度自动化运维专家—TASKCTL
Package management tools
Interview questions for software testing - a collection of interview questions for large factories in 2022
ADB basic commands
js2day(又是i++和++i,if语句,三元运算符,switch、while语句,for循环语句)
Js6day (search, add and delete DOM nodes. Instantiation time, timestamp, timestamp cases, redrawing and reflow)
Unity SKFramework框架(十四)、Extension 扩展函数
百款拿来就能用的网页特效,不来看看吗?
[opencv learning] [template matching]
Mobile layout (flow layout)
JSON serialization and parsing
应用LNK306GN-TL 转换器、非隔离电源
传感器 ADXL335BCPZ-RL7 3轴 加速度计 符合 RoHS/WEEE
std::vector批量导入快速去重方法
8A Synchronous Step-Down regulator tps568230rjer_ Specification information