当前位置:网站首页>记忆函数的性能优化
记忆函数的性能优化
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
}
边栏推荐
- Modular commonjs es module
- [opencv learning] [contour detection]
- moon
- Js7day (event object, event flow, event capture and bubble, prevent event flow, event delegation, student information table cases)
- Linear DP acwing 899 Edit distance
- ADB basic commands
- 阿里初面被两道编程题给干掉,再次内推终上岸(已拿电子offer)
- 8A Synchronous Step-Down regulator tps568230rjer_ Specification information
- Ltc3307ahv meets EMI standard, step-down converter qca7005-al33 phy
- js5day(事件监听,函数赋值给变量,回调函数,环境对象this,全选反选案例,tab栏案例)
猜你喜欢
[opencv learning] [common image convolution kernel]
Browser storage scheme
Unity skframework framework (XVI), package manager development kit Manager
应用LNK306GN-TL 转换器、非隔离电源
JS iterator generator asynchronous code processing promise+ generator - > await/async
bellman-ford AcWing 853. Shortest path with side limit
Unity SKFramework框架(十五)、Singleton 单例
Js6day (search, add and delete DOM nodes. Instantiation time, timestamp, timestamp cases, redrawing and reflow)
Analog to digital converter (ADC) ade7913ariz is specially designed for three-phase energy metering applications
Unity skframework Framework (XVI), package manager Development Kit Manager
随机推荐
Unity skframework framework (XV), singleton singleton
Counter attack of flour dregs: MySQL 66 questions, 20000 words + 50 pictures in detail! A little six
net share
Js2day (also i++ and ++i, if statements, ternary operators, switch, while statements, for loop statements)
腾讯三面:进程写文件过程中,进程崩溃了,文件数据会丢吗?
Rust language document Lite (Part 1) - cargo, output, basic syntax, data type, ownership, structure, enumeration and pattern matching
moon
Efficiency comparison between ArrayList and LinkedList
[opencv learning] [contour detection]
Unity SKFramework框架(十七)、FreeCameraController 上帝视角/自由视角相机控制脚本
Day4 operator, self increasing, self decreasing, logical operator, bit operation, binary conversion decimal, ternary operator, package mechanism, document comment
To bypass obregistercallbacks, you need to drive the signature method
Js8day (rolling event (scroll family), offset family, client family, carousel map case (to be done))
JS generates 4-digit verification code
Oracle from entry to mastery (4th Edition)
自主可控三维云CAD:CrownCAD赋能企业创新设计
Linear DP acwing 897 Longest common subsequence
架构师必须了解的 5 种最佳软件架构模式
屠榜多目标跟踪!BoT-SORT:稳健的关联多行人跟踪
Ltc3307ahv meets EMI standard, step-down converter qca7005-al33 phy