SlideShare a Scribd company logo
THROTTLE & DEBOUNCE 
PATTERNS 
IN WEB APPS 
@ALMIRFILHO
@loopinfinito 
l8p.com.br 
@almirfilho
@loopinfinito 
l8p.com.br 
@almirfilho
@loopinfinito 
l8p.com.br 
@almirfilho 
after 
conf
THE PROBLEM
How to control 
user events 
frequency?
SOME CASES 
onclick 
onresize 
onscroll 
onmousemove
onclick 
Order some shit 
… 
Some AJAX action. Whatever
onclick 
Order some shit 
Some AJAX action. Whatever 
click 
freak
onresize 
Responsive modafoca
onresize 
Δ = 100px 
≃ 100 * 
triggerings! 
!%?#$ 
Responsive modafoca
onscroll 
Paralax bullshit
onscroll 
Δ = 100px 
… same 
fuc*ing 
thing 
≃ 
Paralax bullshit
onmousemove 
Gaming junk
onmousemove 
Δx = 100px 
Δy = 50px 
≃ 150 * 
trigg… OMG 
plz stop 
Gaming junk
**BONUS** PROBLEM
Updating <canvas> 
drawings?
Updating <canvas> 
drawings? 
just redraw 
E-V-E-R-Y-T-H-I-N-G
stage.update = function(){ 
redrawHeavyShit(); 
}; 
! 
while(game.isOn){ 
game.step(); 
stage.update(); 
} 
stupid 
game loop
WAY COOLER 
stage.update = function(){ 
redrawHeavyShit(); 
}; 
! 
var gameLoop = function(){ 
game.step(); 
stage.update(); 
requestAnimationFrame(gameLoop); 
}; 
! 
gameLoop();
WAY COOLER 
stage.update = function(){ 
redrawHeavyShit(); 
}; 
! 
var gameLoop = function(){ 
game.step(); 
stage.update(); 
requestAnimationFrame(gameLoop); 
}; 
! 
gameLoop();
Measuring 
damage with 
dev tools
RENDERING & PAINTING COSTS 
all major and modern* browsers 
* even in IE (11)
So, how to control 
user events 
frequency?
THROTTLE
A throttle is a 
mechanism to 
manage fuel flow 
in an engine
ENGINE THROTTLE
So, throttle is just a 
valve? 
! yeeep
COMMON CASES 
resizing 
scrolling 
mouse moving
0s 0.1s 
t 
onscroll 
E E E E E E E E E E E 
paralax()
onscroll throttled 
0s 0.1s 
t 
E E E E E E E E E E E 
THROTTLE 
paralax()
onscroll throttled 
0s 0.1s 
t 
E E E E E E E E E E E 
THROTTLE 
paralax()
var paralax = function(args){ 
complexHeavyShit(); 
}; 
! 
window.addEventListener(‘scroll’, function(e){ 
paralax(e.args); 
});
LET’S 
THROOOOTLE IT 
var paralax = function(args){ 
complexHeavyShit(); 
}; 
! 
window.addEventListener(‘scroll’, 
throttleParalax() 
);
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); 
} 
}; 
}());
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
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.
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
Let’s visualize it
Let’s visualize it 
0s 500ms 
t 
E 
event 
happens
Let’s visualize it 
0s 500ms 
t 
E 
event 
executes
Let’s visualize it 
0s 500ms 
t 
E 100ms 
timeWindow
Let’s visualize it 
0s 500ms 
t 
E 100ms 
E 
another event 
happens
Let’s visualize it 
0s 500ms 
t 
E 100ms 
E 
no execution
Let’s visualize it 
0s 500ms 
t 
E 100ms 
E E 
event 
happens
Let’s visualize it 
0s 500ms 
t 
E 100ms 
E E 
same thing 
now
Let’s visualize it 
0s 500ms 
t 
E 100ms 
E E 100ms
Let’s visualize it 
0s 500ms 
t 
E 100ms 
E E 1E00msE E
DEBOUNCE
A debouncing is a 
technique to 
guarantee that a 
button was pressed 
only once.
ELECTRONIC 
DEBOUNCING
Debounce cancels 
multiple actions for 
postpone to the 
last one.
COMMON CASES 
clicking 
key pressing
0s 1s 
t 
onkeyup 
E E E E E E E E E 
autoComplete()
onkeyup debouncing 
0s 1s 
t 
E E E E E E E E E 
DEBOUNCE 
autoComplete()
onkeyup debouncing 
0s 1s 
t 
E E E E E E E E E 
DEBOUNCE 
autoComplete()
btn.addEventListener(‘keyup’, function(){ 
autoComplete(); 
});
LET’S 
DEBOOOUNCE IT 
btn.addEventListener(‘keyup’, 
debounceAutoComplete() 
);
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); 
}; 
}());
var debounceAutoComplete = (function(){ 
var timeWindow = 100; 
sets 
var timeout; 
a context 
! 
var autoComplete = function(arg1, arg2){/* … */}; 
! 
return function(){ 
var context = this; 
var args = arguments; 
clearTimeout(timeout); 
timeout = setTimeout(function(){ 
autoComplete.apply(context, args); 
}, timeWindow); 
}; 
}());
var debounceAutoComplete = (function(){ 
var timeWindow = 100; 
var timeout; 
! 
var autoComplete = function(arg1, arg2){/* … */}; 
! 
return function(){ 
sets 
the func. 
var context = this; 
var args = arguments; 
clearTimeout(timeout); 
timeout = setTimeout(function(){ 
autoComplete.apply(context, args); 
}, timeWindow); 
}; 
}());
var debounceAutoComplete = (function(){ 
var timeWindow = 100; 
var timeout; 
! 
var autoComplete = function(arg1, arg2){/* … */}; 
! 
return function(){ 
return the 
handler 
var context = this; 
var args = arguments; 
clearTimeout(timeout); 
timeout = setTimeout(function(){ 
autoComplete.apply(context, args); 
}, timeWindow); 
}; 
}());
Let’s visualize it
Let’s visualize it 
0s 500ms 
t 
E 
event 
happens
Let’s visualize it 
0s 500ms 
t 
E 100ms 
setTimeOut
Let’s visualize it 
0s 500ms 
t 
E 100ms 
E 
another event 
happens
Let’s visualize it 
0s 500ms 
t 
E 100ms 
E 
clearTimeOut
Let’s visualize it 
0s 500ms 
t 
E 100ms 
reset 
timeOut 
E
Let’s visualize it 
0s 500ms 
t 
E E 10E0ms
Let’s visualize it 
0s 500ms 
t 
E E 10E0ms
Let’s visualize it 
0s 500ms 
t 
E E E 100ms
Let’s visualize it 
0s 500ms 
t 
E E E 100ms 
cool to 
execute!
Let’s visualize it 
0s 500ms 
t 
E E E 100ms 
E 
life goes on…
READ ABOUT [PT-BR]
but… 
<x-mimimi>
JQUERY PLUGIN 
jquery-throttle-debounce 
$(window).scroll($.throttle(250, paralax)); 
! 
$('input').keyup($.debounce(250, autoComplete)); 
github.com/cowboy/jquery-throttle-debounce
UNDERSCORE.JS 
$(window).scroll(_.throttle(paralax, 250)); 
! 
$(‘input’).keyup(_.debounce(autoComplete, 250)); 
underscorejs.org
THANK 
YOU! 
@ALMIRFILHO

More Related Content

DOCX
Java Programs
PDF
The Ring programming language version 1.5.3 book - Part 89 of 184
DOC
Tarea De Scilab By Sebastian Vasquez
DOCX
Cuarto Punto Parte A
PDF
RxJS - 封裝程式的藝術
PPT
Menu Driven programs in Java
DOCX
Cuarto Punto Parte B
PDF
如何「畫圖」寫測試 - RxJS Marble Test
Java Programs
The Ring programming language version 1.5.3 book - Part 89 of 184
Tarea De Scilab By Sebastian Vasquez
Cuarto Punto Parte A
RxJS - 封裝程式的藝術
Menu Driven programs in Java
Cuarto Punto Parte B
如何「畫圖」寫測試 - RxJS Marble Test

What's hot (20)

PPTX
Programming ppt files (final)
DOCX
Taller De Scilab
DOCX
Ejercicios Scilab Completo
DOCX
Trabajo Scilab
KEY
Lock? We don't need no stinkin' locks!
PDF
Parametricity - #cljsyd - May, 2015
PDF
SPL 8 | Loop Statements in C
PDF
The Ring programming language version 1.9 book - Part 92 of 210
DOCX
Newton cotes method
PPTX
Concurrent Application Development using Scala
DOCX
PDF
Universal JavaScript
PPTX
2 презентация rx java+android
PDF
You will learn RxJS in 2017
DOCX
Faisal
DOCX
PDF
Csharp_Chap04
DOCX
Quinto Punto Parte B
PDF
New feature of async fakeAsync test in angular
Programming ppt files (final)
Taller De Scilab
Ejercicios Scilab Completo
Trabajo Scilab
Lock? We don't need no stinkin' locks!
Parametricity - #cljsyd - May, 2015
SPL 8 | Loop Statements in C
The Ring programming language version 1.9 book - Part 92 of 210
Newton cotes method
Concurrent Application Development using Scala
Universal JavaScript
2 презентация rx java+android
You will learn RxJS in 2017
Faisal
Csharp_Chap04
Quinto Punto Parte B
New feature of async fakeAsync test in angular
Ad

Similar to Throttle and Debounce Patterns in Web Apps (20)

PDF
Fast Cordova applications
PPTX
JavaScript Timers, Power Consumption, and Performance
PDF
Ten useful JavaScript tips & best practices
PPTX
AppForum 2014 Boost Hybrid App Performance
PDF
Performance.now() fast but not furious
PDF
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
PDF
Fast mobile web apps
PPT
Web development basics (Part-6)
PDF
Google I/O State Of Ajax
PDF
Performance on the Yahoo! Homepage
PDF
Building performance into the new yahoo homepage presentation
PPTX
How to perform debounce in react
PDF
HTML5 APIs - Where no man has gone before! - Altran
KEY
YUI for Mobile - HTML5DevConf 11
PDF
High Performance JavaScript - jQuery Conference SF Bay Area 2010
PDF
PrairieDevCon 2014 - Web Doesn't Mean Slow
PDF
Nicholas' Performance Talk at Google
PDF
Front-end optimisation & jQuery Internals
PDF
jQuery Conference San Diego 2014 - Web Performance
PPTX
The Internet Is Slowing Down, Fast
Fast Cordova applications
JavaScript Timers, Power Consumption, and Performance
Ten useful JavaScript tips & best practices
AppForum 2014 Boost Hybrid App Performance
Performance.now() fast but not furious
JavaScript APIs - The Web is the Platform - MDN Hack Day, Sao Paulo
Fast mobile web apps
Web development basics (Part-6)
Google I/O State Of Ajax
Performance on the Yahoo! Homepage
Building performance into the new yahoo homepage presentation
How to perform debounce in react
HTML5 APIs - Where no man has gone before! - Altran
YUI for Mobile - HTML5DevConf 11
High Performance JavaScript - jQuery Conference SF Bay Area 2010
PrairieDevCon 2014 - Web Doesn't Mean Slow
Nicholas' Performance Talk at Google
Front-end optimisation & jQuery Internals
jQuery Conference San Diego 2014 - Web Performance
The Internet Is Slowing Down, Fast
Ad

More from Almir Filho (7)

PDF
256 Shades of R, G and B
PDF
The Creative Developer
PDF
Esse cara é o grunt
PDF
CSS Layout: O ontem, o hoje e o depois
PDF
Web Audio Hero
PDF
HTML5 Sensitivo: Seu browser no plano astral
PDF
HTML5: seu navegador não é mais o mesmo
256 Shades of R, G and B
The Creative Developer
Esse cara é o grunt
CSS Layout: O ontem, o hoje e o depois
Web Audio Hero
HTML5 Sensitivo: Seu browser no plano astral
HTML5: seu navegador não é mais o mesmo

Recently uploaded (20)

PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
Nekopoi APK 2025 free lastest update
PDF
System and Network Administraation Chapter 3
PDF
top salesforce developer skills in 2025.pdf
PPTX
Online Work Permit System for Fast Permit Processing
PDF
Digital Strategies for Manufacturing Companies
PPTX
L1 - Introduction to python Backend.pptx
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
Transform Your Business with a Software ERP System
PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PPTX
ISO 45001 Occupational Health and Safety Management System
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PPTX
ai tools demonstartion for schools and inter college
PPTX
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PPTX
ManageIQ - Sprint 268 Review - Slide Deck
PPTX
Odoo POS Development Services by CandidRoot Solutions
Internet Downloader Manager (IDM) Crack 6.42 Build 42 Updates Latest 2025
Design an Analysis of Algorithms II-SECS-1021-03
Nekopoi APK 2025 free lastest update
System and Network Administraation Chapter 3
top salesforce developer skills in 2025.pdf
Online Work Permit System for Fast Permit Processing
Digital Strategies for Manufacturing Companies
L1 - Introduction to python Backend.pptx
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Transform Your Business with a Software ERP System
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
ISO 45001 Occupational Health and Safety Management System
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
Adobe Illustrator 28.6 Crack My Vision of Vector Design
ai tools demonstartion for schools and inter college
Agentic AI : A Practical Guide. Undersating, Implementing and Scaling Autono...
PTS Company Brochure 2025 (1).pdf.......
Which alternative to Crystal Reports is best for small or large businesses.pdf
ManageIQ - Sprint 268 Review - Slide Deck
Odoo POS Development Services by CandidRoot Solutions

Throttle and Debounce Patterns in Web Apps