当前位置:网站首页>Performance optimization of memory function
Performance optimization of memory function
2022-07-02 13:16:00 【1900's 88 keys】
Find the factorial
Generally, the method of finding factorial
function factorial(n) {
if (n == 0 || n == 1) return 1;
return n * factorial(n - 1)
}
console.time("factorial");
factorial(3000)
console.timeEnd("factorial");

You can see that the direct running time is about 0.3s about
adopt hash Table for caching
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");

You can find that the first time you call 0.9s The second call is 0.01s
Higher order memory function
Abstract the process into a function to make it universal
function memorize(fn) {
let cache = {
};
return function () {
let key = fn.name + '_' + [].join.call(arguments, ','); // Function names and parameters are spliced
// 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");

Implementing caching
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
}
边栏推荐
- 面渣逆袭:MySQL六十六问,两万字+五十图详解!有点六
- Answer: can the audio be set to on by default during easydss video on demand?
- Fully autonomous and controllable 3D cloud CAD: crowncad's convenient command search can quickly locate the specific location of the required command.
- Day4 operator, self increasing, self decreasing, logical operator, bit operation, binary conversion decimal, ternary operator, package mechanism, document comment
- [opencv learning] [moving object detection]
- 机器学习基础(二)——训练集和测试集的划分
- What are eNB, EPC and PGW?
- Jerry's watch modifies the alarm clock [chapter]
- Unity SKFramework框架(十四)、Extension 扩展函数
- Std:: vector batch import fast de duplication method
猜你喜欢

Crowncad (crown CAD), the first fully independent 3D CAD platform based on Cloud Architecture in China

Sensor adxl335bcpz-rl7 3-axis accelerometer complies with rohs/weee

mac(macos Monterey12.2 m1) 个人使用php开发

Ltc3307ahv meets EMI standard, step-down converter qca7005-al33 phy

Redis数据库持久化

Essential for operation and maintenance - Elk log analysis system

无向图的桥

【云原生数据库】遇到慢SQL该怎么办(上)?

Everyone wants to eat a broken buffet. It's almost cold

三面阿里,有惊无险成功拿到offer定级P7,只能说是真的难
随机推荐
Apply lnk306gn-tl converter, non isolated power supply
To bypass obregistercallbacks, you need to drive the signature method
Unity skframework framework (XIV), extension extension function
JS逆向之巨量创意signature签名
Unity SKFramework框架(十二)、Score 计分模块
Explain in detail the process of realizing Chinese text classification by CNN
numpy数组计算
[opencv] [image gradient]
国内首款、完全自主、基于云架构的三维CAD平台——CrownCAD(皇冠CAD)
3 a VTT terminal regulator ncp51200mntxg data
Jerry's watch time synchronization [chapter]
研究表明“气味相投”更易成为朋友
Linear DP acwing 899 Edit distance
(7) Web security | penetration testing | how does network security determine whether CND exists, and how to bypass CND to find the real IP
Finally, someone explained the supervised learning clearly
互联网常见34个术语解释
Jerry's weather code table [chapter]
js1day(輸入輸出語法,數據類型,數據類型轉換,var和let區別)
West digital decided to raise the price of flash memory products immediately after the factory was polluted by materials
Web Foundation