Transcript
Page 1: Throttle and Debounce Patterns in Web Apps

PATTERNS IN WEB APPS

THROTTLE &

DEBOUNCE

@ALMIRFILHO

Page 2: Throttle and Debounce Patterns in Web Apps

@almirfilho

@loopinfinito

l8p.com.br

Page 3: Throttle and Debounce Patterns in Web Apps

@almirfilho

@loopinfinito

l8p.com.br

Page 4: Throttle and Debounce Patterns in Web Apps

@almirfilho

@loopinfinito

l8p.com.br

after conf

Page 5: Throttle and Debounce Patterns in Web Apps

THE PROBLEM

Page 6: Throttle and Debounce Patterns in Web Apps

How to control user events frequency?

Page 7: Throttle and Debounce Patterns in Web Apps

onclick onresize onscroll onmousemove

SOME CASES

Page 8: Throttle and Debounce Patterns in Web Apps

onclickOrder some shit

Some AJAX action. Whatever

Page 9: Throttle and Debounce Patterns in Web Apps

onclickOrder some shit

Some AJAX action. Whatever

click

freak

Page 10: Throttle and Debounce Patterns in Web Apps

onresizeResponsive modafoca

Page 11: Throttle and Debounce Patterns in Web Apps

onresize

∆ = 100px

triggerings! !%?#$

≃ 100 *

Responsive modafoca

Page 12: Throttle and Debounce Patterns in Web Apps

onscrollParalax bullshit

Page 13: Throttle and Debounce Patterns in Web Apps

onscroll

∆ = 100px… same fuc*ing

thing

Paralax bullshit

Page 14: Throttle and Debounce Patterns in Web Apps

onmousemoveGaming junk

Page 15: Throttle and Debounce Patterns in Web Apps

onmousemove

∆x = 100px ∆y = 50px

trigg… OMG plz stop

≃ 150 *

Gaming junk

Page 16: Throttle and Debounce Patterns in Web Apps

**BONUS** PROBLEM

Page 17: Throttle and Debounce Patterns in Web Apps

Updating <canvas> drawings?

Page 18: Throttle and Debounce Patterns in Web Apps

just redraw E-V-E -R -Y-T-H- I -N-G

Updating <canvas> drawings?

Page 19: Throttle and Debounce Patterns in Web Apps

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

while(game.isOn){ game.step(); stage.update(); }

stupid game loop

Page 20: Throttle and Debounce Patterns in Web Apps

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

WAY COOLER

Page 21: Throttle and Debounce Patterns in Web Apps

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

WAY COOLER

Page 22: Throttle and Debounce Patterns in Web Apps

Measuring damage with

dev tools

Page 23: Throttle and Debounce Patterns in Web Apps

RENDERING & PAINTING COSTS

all major and modern* browsers * even in IE (11)

Page 24: Throttle and Debounce Patterns in Web Apps

So, how to control user events frequency?

Page 25: Throttle and Debounce Patterns in Web Apps

THROTTLE

Page 26: Throttle and Debounce Patterns in Web Apps

A throttle is a mechanism to

manage fuel flow in an engine

Page 27: Throttle and Debounce Patterns in Web Apps

ENGINE THROTTLE

Page 28: Throttle and Debounce Patterns in Web Apps

So, throttle is just a valve?

!yeeep

Page 29: Throttle and Debounce Patterns in Web Apps

resizing scrolling

mouse moving

COMMON CASES

Page 30: Throttle and Debounce Patterns in Web Apps

tE E E E E E E E E E E

onscroll

paralax()

0.1s0s

Page 31: Throttle and Debounce Patterns in Web Apps

tE E E E E E E E E E E

onscroll throttled

paralax()

0.1s0s

THROTTLE

Page 32: Throttle and Debounce Patterns in Web Apps

tE E E E E E E E E E E

onscroll throttled

paralax()

0.1s0s

THROTTLE

Page 33: Throttle and Debounce Patterns in Web Apps

var paralax = function(args){ complexHeavyShit(); }; !

window.addEventListener(‘scroll’, function(e){ paralax(e.args); });

Page 34: Throttle and Debounce Patterns in Web Apps

var paralax = function(args){ complexHeavyShit(); }; !

window.addEventListener(‘scroll’, throttleParalax() );

LET’S THROOOOTLE IT

Page 35: Throttle and Debounce Patterns in Web Apps

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); } }; }());

Page 36: Throttle and Debounce Patterns in Web Apps

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

Page 37: Throttle and Debounce Patterns in Web Apps

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.

Page 38: Throttle and Debounce Patterns in Web Apps

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

Page 39: Throttle and Debounce Patterns in Web Apps

Let’s visualize it

Page 40: Throttle and Debounce Patterns in Web Apps

tE

500ms0s

event

happens

Let’s visualize it

Page 41: Throttle and Debounce Patterns in Web Apps

t

500ms0s

E

Let’s visualize it

event executes

Page 42: Throttle and Debounce Patterns in Web Apps

t

500ms0s

100msE

timeWindow

Let’s visualize it

Page 43: Throttle and Debounce Patterns in Web Apps

t

500ms0s

100msE

Let’s visualize it

another event

happens

E

Page 44: Throttle and Debounce Patterns in Web Apps

t

500ms0s

100msE

Let’s visualize it

no execution

E

Page 45: Throttle and Debounce Patterns in Web Apps

t

500ms0s

100msE

Let’s visualize it

event

happens

E E

Page 46: Throttle and Debounce Patterns in Web Apps

t

500ms0s

100msE

Let’s visualize it

same thing

now

E E

Page 47: Throttle and Debounce Patterns in Web Apps

t

500ms0s

100msE

Let’s visualize itE 100msE

Page 48: Throttle and Debounce Patterns in Web Apps

t

500ms0s

100msE

Let’s visualize itE 100msE E E E

Page 49: Throttle and Debounce Patterns in Web Apps

DEBOUNCE

Page 50: Throttle and Debounce Patterns in Web Apps

A debouncing is a technique to

guarantee that a button was pressed

only once.

Page 51: Throttle and Debounce Patterns in Web Apps

ELECTRONIC DEBOUNCING

Page 52: Throttle and Debounce Patterns in Web Apps

Debounce cancels multiple actions for postpone to the

last one.

Page 53: Throttle and Debounce Patterns in Web Apps

clicking key pressing

COMMON CASES

Page 54: Throttle and Debounce Patterns in Web Apps

tE E E E E E E E E

onkeyup

autoComplete()

1s0s

Page 55: Throttle and Debounce Patterns in Web Apps

tE E E E E E E E E

onkeyup debouncing1s0s

DEBOUNCE

autoComplete()

Page 56: Throttle and Debounce Patterns in Web Apps

tE E E E E E E E E

onkeyup debouncing1s0s

DEBOUNCE

autoComplete()

Page 57: Throttle and Debounce Patterns in Web Apps

btn.addEventListener(‘keyup’, function(){ autoComplete(); });

Page 58: Throttle and Debounce Patterns in Web Apps

btn.addEventListener(‘keyup’, debounceAutoComplete() );

LET’S DEBOOOUNCE IT

Page 59: Throttle and Debounce Patterns in Web Apps

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); }; }());

Page 60: Throttle and Debounce Patterns in Web Apps

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

Page 61: Throttle and Debounce Patterns in Web Apps

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.

Page 62: Throttle and Debounce Patterns in Web Apps

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

Page 63: Throttle and Debounce Patterns in Web Apps

Let’s visualize it

Page 64: Throttle and Debounce Patterns in Web Apps

tE

500ms0s

event

happens

Let’s visualize it

Page 65: Throttle and Debounce Patterns in Web Apps

t

500ms0s

100msE

setTimeOut

Let’s visualize it

Page 66: Throttle and Debounce Patterns in Web Apps

t

500ms0s

100msE

another event

happens

E

Let’s visualize it

Page 67: Throttle and Debounce Patterns in Web Apps

t

500ms0s

100msE

clearTimeOut

E

Let’s visualize it

Page 68: Throttle and Debounce Patterns in Web Apps

t

500ms0s

100msE

reset

timeOut

E

Let’s visualize it

Page 69: Throttle and Debounce Patterns in Web Apps

t

500ms0s

100msE E E

Let’s visualize it

Page 70: Throttle and Debounce Patterns in Web Apps

t

500ms0s

100msE E E

Let’s visualize it

Page 71: Throttle and Debounce Patterns in Web Apps

t

500ms0s

100msE E E

Let’s visualize it

Page 72: Throttle and Debounce Patterns in Web Apps

t

500ms0s

100msE E E

cool to execute!

Let’s visualize it

Page 73: Throttle and Debounce Patterns in Web Apps

t

500ms0s

100msE E E

life goes on…

E

Let’s visualize it

Page 74: Throttle and Debounce Patterns in Web Apps

READ ABOUT [PT-BR]

Page 75: Throttle and Debounce Patterns in Web Apps

but… <x-mimimi>

Page 76: Throttle and Debounce Patterns in Web Apps

$(window).scroll($.throttle(250, paralax)); !

$('input').keyup($.debounce(250, autoComplete));

JQUERY PLUGINjquery-throttle-debounce

github.com/cowboy/jquery-throttle-debounce

Page 77: Throttle and Debounce Patterns in Web Apps

UNDERSCORE.JS

underscorejs.org

$(window).scroll(_.throttle(paralax, 250)); !

$(‘input’).keyup(_.debounce(autoComplete, 250));

Page 78: Throttle and Debounce Patterns in Web Apps

THANK YOU!

@ALMIRFILHO


Top Related