当前位置:网站首页>记忆函数的性能优化
记忆函数的性能优化
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
}
边栏推荐
- Explain in detail the process of realizing Chinese text classification by CNN
- PXE installation UOS prompt NFS over TCP not available from 10 x.x.x
- Everyone wants to eat a broken buffet. It's almost cold
- Domestic free data warehouse ETL dispatching automation operation and maintenance expert taskctl
- 百款拿来就能用的网页特效,不来看看吗?
- The coloring method determines the bipartite graph acwing 860 Chromatic judgement bipartite graph
- 绕过ObRegisterCallbacks需要驱动签名方法
- Js5day (event monitoring, function assignment to variables, callback function, environment object this, select all, invert selection cases, tab column cases)
- Unity SKFramework框架(十七)、FreeCameraController 上帝视角/自由视角相机控制脚本
- Unity skframework framework (XVIII), roamcameracontroller roaming perspective camera control script
猜你喜欢

VIM super practical guide collection of this one is enough

Finally, someone explained the supervised learning clearly
![JDBC prevent SQL injection problems and solutions [preparedstatement]](/img/32/f71f5a31cdf710704267ff100b85d7.png)
JDBC prevent SQL injection problems and solutions [preparedstatement]

LTC3307AHV 符合EMI标准,降压转换器 QCA7005-AL33 PHY

Hash table acwing 840 Simulated hash table

Unity skframework framework (XX), VFX lab special effects library

Modular commonjs es module

Floyd AcWing 854. Floyd finds the shortest path

自主可控三维云CAD:CrownCAD赋能企业创新设计

Unity SKFramework框架(十四)、Extension 扩展函数
随机推荐
Linear DP acwing 897 Longest common subsequence
国产免费数据仓库ETL调度自动化运维专家—TASKCTL
Package management tools
阿里发布的Redis开发文档,涵盖了所有的redis操作
Docsify deploy IIS
Linear DP acwing 896 Longest ascending subsequence II
OLED screen driver based on stm32
Fully autonomous and controllable 3D cloud CAD: crowncad's convenient command search can quickly locate the specific location of the required command.
js5day(事件监听,函数赋值给变量,回调函数,环境对象this,全选反选案例,tab栏案例)
Linear DP acwing 895 Longest ascending subsequence
std::vector批量导入快速去重方法
Unity SKFramework框架(十五)、Singleton 单例
Word efficiency guide - word's own template
模数转换器(ADC) ADE7913ARIZ 专为三相电能计量应用而设计
net share
Finally, someone explained the supervised learning clearly
Everyone wants to eat a broken buffet. It's almost cold
Typora+docsify quick start
Oracle从入门到精通(第4版)
Do you know all the interface test interview questions?