Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Throttle & Debounce Patterns in Web Apps

Throttle & Debounce Patterns in Web Apps

A brief discussion on the Throttle and Debounce Patterns. Where, when and why to use them? They solve some problems that may harm the performance of an entire web app due to misuse of user events.

Almir Filho

July 26, 2014
Tweet

More Decks by Almir Filho

Other Decks in Programming

Transcript

  1. stage.update = function(){ redrawHeavyShit(); }; ! var gameLoop = function(){

    game.step(); stage.update(); requestAnimationFrame(gameLoop); }; ! gameLoop(); WAY COOLER
  2. stage.update = function(){ redrawHeavyShit(); }; ! var gameLoop = function(){

    game.step(); stage.update(); requestAnimationFrame(gameLoop); }; ! gameLoop(); WAY COOLER
  3. t E E E E E E E E E

    E E onscroll paralax() 0.1s 0s
  4. t E E E E E E E E E

    E E onscroll throttled paralax() 0.1s 0s THROTTLE
  5. t E E E E E E E E E

    E E onscroll throttled paralax() 0.1s 0s THROTTLE
  6. var throttleParalax = (function(){ var timeWindow = 500; var now

    = (new Date()).getTime(); var lastExecution = new Date(now - timeWindow); ! var paralax = function(args){ complexHeavyShit(); }; ! return function(){ var now = (new Date()).getTime(); if(lastExecution.getTime() + timeWindow <= now){ lastExecution = new Date(); return paralax.apply(this, arguments); } }; }());
  7. var throttleParalax = (function(){ var timeWindow = 500; var now

    = (new Date()).getTime(); var lastExecution = new Date(now - timeWindow); ! var paralax = function(args){ complexHeavyShit(); }; ! return function(){ var now = (new Date()).getTime(); if(lastExecution.getTime() + timeWindow <= now){ lastExecution = new Date(); return paralax.apply(this, arguments); } }; }()); sets a context
  8. var throttleParalax = (function(){ var timeWindow = 500; var now

    = (new Date()).getTime(); var lastExecution = new Date(now - timeWindow); ! var paralax = function(args){ complexHeavyShit(); }; ! return function(){ var now = (new Date()).getTime(); if(lastExecution.getTime() + timeWindow <= now){ lastExecution = new Date(); return paralax.apply(this, arguments); } }; }()); sets the func.
  9. var throttleParalax = (function(){ var timeWindow = 500; var now

    = (new Date()).getTime(); var lastExecution = new Date(now - timeWindow); ! var paralax = function(args){ complexHeavyShit(); }; ! return function(){ var now = (new Date()).getTime(); if(lastExecution.getTime() + timeWindow <= now){ lastExecution = new Date(); return paralax.apply(this, arguments); } }; }()); returns the event handler
  10. t E E E E E E E E E

    onkeyup autoComplete() 1s 0s
  11. t E E E E E E E E E

    onkeyup debouncing 1s 0s DEBOUNCE autoComplete()
  12. t E E E E E E E E E

    onkeyup debouncing 1s 0s DEBOUNCE autoComplete()
  13. var debounceAutoComplete = (function(){ var timeWindow = 100; var timeout;

    ! var autoComplete = function(arg1, arg2){/* … */}; ! return function(){ var context = this; var args = arguments; clearTimeout(timeout); timeout = setTimeout(function(){ autoComplete.apply(context, args); }, timeWindow); }; }());
  14. var debounceAutoComplete = (function(){ var timeWindow = 100; var timeout;

    ! var autoComplete = function(arg1, arg2){/* … */}; ! return function(){ var context = this; var args = arguments; clearTimeout(timeout); timeout = setTimeout(function(){ autoComplete.apply(context, args); }, timeWindow); }; }()); sets a context
  15. var debounceAutoComplete = (function(){ var timeWindow = 100; var timeout;

    ! var autoComplete = function(arg1, arg2){/* … */}; ! return function(){ var context = this; var args = arguments; clearTimeout(timeout); timeout = setTimeout(function(){ autoComplete.apply(context, args); }, timeWindow); }; }()); sets the func.
  16. var debounceAutoComplete = (function(){ var timeWindow = 100; var timeout;

    ! var autoComplete = function(arg1, arg2){/* … */}; ! return function(){ var context = this; var args = arguments; clearTimeout(timeout); timeout = setTimeout(function(){ autoComplete.apply(context, args); }, timeWindow); }; }()); return the handler