当前位置:网站首页>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
}
边栏推荐
- Operation tutorial: how does easydss convert MP4 on demand files into RTSP video streams?
- Everyone wants to eat a broken buffet. It's almost cold
- 三翼鸟两周年:羽翼渐丰,腾飞指日可待
- [opencv learning] [contour detection]
- MySQL: Invalid GIS data provided to function st_ geometryfromtext
- 国内首款、完全自主、基于云架构的三维CAD平台——CrownCAD(皇冠CAD)
- Mobile layout (flow layout)
- [opencv learning] [moving object detection]
- Research shows that "congenial" is more likely to become friends
- 自主可控三维云CAD:CrownCAD赋能企业创新设计
猜你喜欢
We sincerely invite young creators to share with investors and entrepreneurs how to make choices in life in the metauniverse
Ali was killed by two programming problems at the beginning, pushed inward again, and finally landed (he has taken an electronic offer)
2、 Frame mode MPLS operation
Unity skframework framework (XXI), texture filter map resource filtering tool
Lucky numbers in the [leetcode daily question] matrix
TVOC, VOC, VOCs gas detection + Solution
Professor of Shanghai Jiaotong University: he Yuanjun - bounding box (containment / bounding box)
Fundamentals of face recognition (facenet)
OpenAPI generator: simplify the restful API development process
Crowncad (crown CAD), the first fully independent 3D CAD platform based on Cloud Architecture in China
随机推荐
Variable, "+" sign, data type
Get started REPORT | today, talk about the microservice architecture currently used by Tencent
伙伴云表格强势升级!Pro版,更非凡!
JS逆向之行行查data解密
Unity SKFramework框架(十六)、Package Manager 開發工具包管理器
Js4day (DOM start: get DOM element content, modify element style, modify form element attributes, setinterval timer, carousel Map Case)
挥发性有机物TVOC、VOC、VOCS气体检测+解决方案
Unity skframework framework (XIII), question module
8A Synchronous Step-Down regulator tps568230rjer_ Specification information
C modifier
Oracle from entry to mastery (4th Edition)
Linear DP acwing 899 Edit distance
Structured data, semi-structured data and unstructured data
Mobile layout (flow layout)
Essential for operation and maintenance - Elk log analysis system
难忘阿里,4面技术5面HR附加笔试面,走的真艰难真心酸
Web基础
2、 Frame mode MPLS operation
Unity skframework framework (XX), VFX lab special effects library
Jerry's watch modifies the alarm clock [chapter]