当前位置:网站首页>Function throttling for performance optimization in JS
Function throttling for performance optimization in JS
2022-06-09 17:58:00 【Front end tripod】
Definition
Limit the function execution to one time within the specified time , Ignore the extra triggers .
It can also be directly understood that throttling means saving flow ( Code execution overhead ), Reduce the original high-frequency operation to the time frequency we set .
Applicable scenario
From the definition, throttling mainly reduces the frequency of function execution , Save the execution cost without delaying the normal function implementation , So most of the scenarios it is applicable to are those with extremely high trigger frequency , If we do not process and there are some expensive operations in the triggered function ( For example, a large number of operations dom Or go ahead ajax Request etc. ) It may block the browser performance and cause a jam . for example :
1、 Browser window change event window.onresize
When the size of the browser window changes onresize Events are triggered very frequently .
2、 mousemove event
When we bind the drag event , This event will also be triggered frequently when dragging .
3、 scroll event
Window scrolling events are also frequently triggered when scrolling .
Realization principle
It can be seen from the above that the cause of the problem is that the function is triggered too frequently , Doing unnecessary operations , So we just need to artificially reduce the trigger frequency to the appropriate frequency . For example, when dragging an event , If you want to initiate a request, it may be enough to send it once or twice a second , Then we can ignore other unnecessary requests according to time .
Code implementation
There are many code implementations for function throttling , The following will be used setTimeout The timer delays execution for a period of time . The specific code is as follows :
/**
*
* @param {*} callback Functions to execute
* @param {*} time Function execution interval
*
*/
const saveTraffic = (callback, time) => {
let timer,
firstTime;
return function(...res) {
const _this = this;
// The first call can be executed directly
if (firstTime) {
callback.apply(_this, res);
return firstTime = false;
};
if (timer) return;
timer = setTimeout(() => {
clearTimeout(timer);
timer = null;
callback.apply(_this, res);
}, time || 500)
}
}
In the above code, the function to be executed is used as setTimeout The timer is delayed for a period of time , If the delayed execution has not been completed , Then ignore the trigger that calls the function next .
test
window.onresize = saveTraffic(function(res) {
console.log(1111, res)
}, 300);
边栏推荐
- nlp网络中两种残差结构对网络的影响
- UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xad in position 2: illegal multibyte sequence
- MySQL 8.0.29 解压版安装配置方法图文教程
- 入驻快讯|欢迎XCHARGE智充科技正式入驻 InfoQ 写作平台!
- 刷脸认证如何实现人脸又快又准完成校验?
- Macro definition CV with parameters in opencv_ Role of assert()
- Construction of sheep (rare species) bsgenome reference genome
- What are the main applications of conductive slip rings
- slurm program running without multiprocess instead run individually
- Application of slip ring in automatic control system
猜你喜欢

Epigentek hi fi cDNA synthesis kit instructions

隔空手势交互,在现实世界上演“得心应手”

Imshow() of OpenCV to view the pixel value of the picture

Operating instructions for abbexa AEC chromogen Kit

How to build a real-time development platform to deeply release the value of enterprise real-time data?

NLP-文本表示-词袋模型和TF-IDF

idea卸载教程

Redis知识点&面试题总结

OpenCV中的带参数宏定义CV_Assert()的作用

解决Chrome提示:“您的连接不是私密连接“,且无继续访问选项问题
随机推荐
同花顺开户安全吗?如何办理开户呢?
如何实现自定义富文本编辑器标签
Solve the chrome prompt: "your connection is not a private connection" and there is no continue access option
自动化控制系统中滑环的应用
妹妹们坐船头,哥哥们岸上走
Operating instructions for abbexa AEC chromogen Kit
js中性能优化之函数节流
C language to solve the problem of climbing stairs
Android 缓存机制 LRUCache
NLP - 关键词提取 - TextRank
如何建设实时开发平台,深入释放企业实时数据价值?
UnicodeDecodeError: ‘gbk‘ codec can‘t decode byte 0xad in position 2: illegal multibyte sequence
MySQL 8.0.29 decompressed version installation and configuration method graphic tutorial
华为云原生之数据仓库服务GaussDB(DWS)的深度使用与应用实践【这次高斯不是数学家】
Side B of the charging pile is Not only official account? And smart charging applet!
Abbexa AEC 色原试剂盒使用说明
演练-分组和聚合函数的组合使用
Redis知识点&面试题总结
14届数独-真题标准数独-Day 6-20220121(补)
文心 ERNIE 3.0加持!小样本也可实现全量数据99%的效果!