JavaScript performance optimization techniques-function throttling

Source: Internet
Author: User

The main idea of function throttling technology is to block repeated function calls through a timer. For functions we use internally, this technology is generally of little significance and is not recommended. It may lose processing of some data. However, it makes sense for functions called on the user interface. For example, a listening function for a mousemove or resize event in IE.

This type of event listening function is often accompanied by two main features: 1. Repeated triggering in a short period of time; 2. A large number of DOM operations. As we all know, DOM operations have a high overhead on memory and CPU, especially when both feature 1 is met, it often puts a lot of pressure on browsers. The function throttling technology is used to reduce the frequency of function calls and improve the performance.

The general mode of this technique is as follows:

 
 
  1. Var processor = {
  2. TimeoutId: null,
  3.  
  4. PerformProcessing: function (){
  5. // Code to be executed
  6. },
  7.  
  8. Process: function (){
  9. ClearTimeout (this. timeoutId );
  10. This. timeoutId = setTimeout (function (){
  11. Processor. Merge mprocessing ();
  12. },100 );
  13. }
  14. };
  15.  
  16. // Call
  17. Processor. process ();
  18. PerformProcessing is the function to be called, and the program entry is in process. Every time you enter process, the function that you actually want to call
  19. PerformProcessing will be executed with a latency of 100 milliseconds. If process is called again during this period, the previous timer will be reset,
  20. Re-start timing. This ensures that the code in the receivmprocessing can be executed at least once every 100 milliseconds. The principle is very simple,
  21. The following function uses this principle and achieves the same purpose through the closure. It accepts two parameters. The first is the function to be actually executed, and the second is the interval.
  22. Function throttle (fn, delay ){
  23. Var timer = null;
  24. Return function (){
  25. Var context = this, args = arguments;
  26. ClearTimeout (timer );
  27. Timer = setTimeout (function (){
  28. Fn. apply (context, args );
  29. }, Delay );
  30. };
  31. }

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.