๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐Ÿ’๊ณต๋ถ€/JS

[JavaScript] ๋””๋ฐ”์šด์Šค(Debounce) vs ์Šค๋กœํ‹€(Throttle)

by rindev 2021. 10. 12.

โœ… ๊ณตํ†ต์ 

  • ์งง์€ ์‹œ๊ฐ„์— ์—ฌ๋Ÿฌ๋ฒˆ ๋ฐœ์ƒํ•˜๋Š” ์ด๋ฒคํŠธ๋ฅผ ๊ทธ๋ฃนํ™”ํ•ด์„œ, ๊ณผ๋„ํ•œ ์ด๋ฒคํŠธ ํ˜ธ์ถœ์„ ๋ฐฉ์ง€
    • ์—„์ฒญ๋‚˜๊ฒŒ ๋น ๋ฅธ์†๋„๋กœ ํด๋ฆญ์„ ์—ฌ๋Ÿฌ๋ฒˆ ํ•˜๊ธฐ
    • ๊ฒ€์ƒ‰์ฐฝ์— ํ‚ค๋ณด๋“œ๋กœ ์ž…๋ ฅํ•˜๊ธฐ
    • ์Šคํฌ๋กค๋ฐ”๋ฅผ ์œ„์•„๋ž˜๋กœ ์ญ‰์ญ‰ ์›€์ง์ด๊ธฐ
    • ์ฐฝ์„ ๋ฆฌ์‚ฌ์ด์ง•ํ•˜๊ธฐ
    • ๋“ฑ๋“ฑ...

 

โœ… ๋””๋ฐ”์šด์Šค (Debounce)

  • ์š”์ฒญ์ด ๋“ค์–ด์˜ค๋ฉด ์ผ์ •์‹œ๊ฐ„์„ ๊ธฐ๋‹ค๋ฆฐ ํ›„ ์š”์ฒญ์„ ์ˆ˜ํ–‰ํ•˜๋Š”๋ฐ,
    ๊ธฐ๋‹ค๋ฆฌ๋Š” ๋™์•ˆ ๊ฐ™์€ ์š”์ฒญ์ด ์ถ”๊ฐ€๋กœ ๋“ค์–ด์˜ค๋ฉด ์ด์ „ ์š”์ฒญ์€ ์ทจ์†Œ๋จ
    • ์—ฐ์ด์–ด์„œ ํ˜ธ์ถœ๋˜๋Š” ํ•จ์ˆ˜ ์ค‘ ๊ฐ€์žฅ ๋งˆ์ง€๋ง‰(๋˜๋Š” ๊ฐ€์žฅ ์ฒ˜์Œ)๋งŒ ํ˜ธ์ถœ ๋˜๋„๋ก ํ•จ
    • ์‰ผ์—†์ด ๊ณ„~~~์† ํด๋ฆญํ•˜๋ฉด ํด๋ฆญ์ด ๋ฉˆ์ถœ๋•Œ๊นŒ์ง€ ๊ธฐ๋‹ค๋ ธ๋‹ค๊ฐ€ ๋ฉˆ์ถ”๋ฉด ์‹คํ–‰ํ•จ
    • ๊ทธ๋ž˜์„œ ๋งŒ์•ฝ ๋ฌดํ•œ ์ž…๋ ฅ์„ ํ•˜๋ฉด ๋ฌดํ•œ ๊ธฐ๋‹ค๋ฆผ์„ ํ•จ
  • toggle ๋ฒ„ํŠผ ์ด๋ฒคํŠธ ๋“ฑ์— ์ฃผ๋ฉด ์ข‹์Œ

 

function debounce(callback, limit = 100) {
    let timeout
    return function(...args) {
        clearTimeout(timeout)
        timeout = setTimeout(() => {
            callback.apply(this, args)
        }, limit)
    }
}


//์‹คํ–‰
inputDebounce.addEventListener("keyup", debounce(function() {
    dispDebounce.textContent = ++debounceCount
}, 100))

์ฝ”๋“œ ์ถœ์ฒ˜ : http://yoonbumtae.com/?p=3584

 

  • callback : ์‹คํ–‰ ๋Œ€์ƒ์ธ ์ฝœ๋ฐฑ ํ•จ์ˆ˜
  • limit : ์–ผ๋งˆ ํ›„์— ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•  ์ง€ ๊ฒฐ์ • (millisecond)
    • limit ์ด๋‚ด์— ํ•จ์ˆ˜๊ฐ€ ๋ฐ˜๋ณต ํ˜ธ์ถœ ๋  ๊ฒฝ์šฐ clearTimeout ์‹คํ–‰ → callback์ด ์‹คํ–‰๋˜์ง€ ์•Š์Œ
    • ํ•จ์ˆ˜์˜ ๋ฐ˜๋ณต ํ˜ธ์ถœ์ด ๋ฉˆ์ถ˜ ํ›„์—๋Š” clearTimeout ์‹คํ–‰ํ•˜์ง€ ์•Š์Œ → limit ๋ฐ€๋ฆฌ์ดˆ ํ›„์— callback.apply ์‹คํ–‰
  • apply๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ this์˜ ๋ฒ”์œ„๋ฅผ ์ฝœ๋ฐฑํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋  ๋•Œ ๊ทธ ์ปจํ…์ŠคํŠธ์˜ this๋กœ ๋งŒ๋“ฌ

 

 

โœ… ์Šค๋กœํ‹€ (Throttle)

  • ์ผ์ • ์‹œ๊ฐ„ ๋™์•ˆ ๋“ค์–ด์˜ค๋Š” ์š”์ฒญ์„ ๋‹ค ๋ชจ์•„์„œ, ๋‹จ ํ•œ๋ฒˆ๋งŒ ์ˆ˜ํ–‰๋˜๋„๋ก ํ•จ
    • ์„ค์ • ์‹œ๊ฐ„์„ 1s๋กœ ํ•˜๋ฉด, 1์ดˆ ๋™์•ˆ ์ตœ๋Œ€ 1๋ฒˆ๋งŒ ์‹คํ–‰๋จ
    • ์‰ผ์—†์ด ๊ณ„~~์† ํด๋ฆญํ•ด๋„ 1์ดˆ์— 1๋ฒˆ์”ฉ๋งŒ ์‹คํ–‰์ด ๋œ๋‹ค
    • ๋ฌดํ•œ ์ž…๋ ฅ์„ ํ•˜๋ฉด ์ •๊ธฐ์ ์œผ๋กœ ์‹คํ–‰ ํ•จ
  • ๋ฌดํ•œ์Šคํฌ๋กค(์Šคํฌ๋กค์„ ๋‚ด๋ฆด ๋•Œ api์—์„œ ์ƒˆ ๊ธ€์„ ๋ถˆ๋Ÿฌ์˜ด)์— ์“ฐ๋ฉด ์ข‹์„ ๊ฒƒ ๊ฐ™์Œ
    • ์‚ฌ์šฉ์ž๊ฐ€ ์Šคํฌ๋กค ๋‚ด๋ฆฌ๋Š” ๊ฒƒ์„ ๋ฉˆ์ถœ๋•Œ ๊นŒ์ง€ ๊ธฐ๋‹ค๋ฆฌ๋Š” ๊ฒƒ ๋ณด๋‹ค, ๋‚ด๋ฆฌ๋Š” ๋™์•ˆ์—๋„ ์ชผ๋”์”ฉ ์ƒˆ ๊ธ€์„ ๋ถˆ๋Ÿฌ์˜ค๋Š” ํŽธ์ด ์ข‹์œผ๋ฏ€๋กœ

 

function throttle(callback, limit = 100) {
    let waiting = false
    return function() {
        if(!waiting) {
            callback.apply(this, arguments)
            waiting = true
            setTimeout(() => {
                waiting = false
            }, limit)
        }
    }
}


//์‹คํ–‰
window.addEventListener("mousemove", throttle(() => {
    dispThrottle.textContent = ++mouseThrottleCount
}, 500))

์ฝ”๋“œ ์ถœ์ฒ˜ : http://yoonbumtae.com/?p=3584

 

  • callback : ์‹คํ–‰ ๋Œ€์ƒ์ธ ์ฝœ๋ฐฑ ํ•จ์ˆ˜
  • limit : ์–ผ๋งˆ ํ›„์— ํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•  ์ง€ ๊ฒฐ์ • (millisecond)
  • waiting : true์ธ ๊ฒฝ์šฐ if๋ฌธ์„ ํƒ€์ง€ ์•Š์Œ
    • ์ตœ์ดˆ ํ•จ์ˆ˜๊ฐ€ ํ˜ธ์ถœ๋  ์‹œ, waiting์€ false์ด๋ฏ€๋กœ if๋ฌธ์„ ํƒ → ์ด ๋•Œ ์ฝœ๋ฐฑํ•จ์ˆ˜๋ฅผ ์‹คํ–‰ํ•œ ๋’ค waiting = true๋กœ ๋ฐ”๊ฟˆ
    • waiting์ด true๊ฐ€ ๋˜์—ˆ์„ ๋•Œ, limit๋ฐ€๋ฆฌ์ดˆ ํ›„์— waiting = false๋กœ ๋ณ€๊ฒฝ๋˜๋ฉฐ, ์ฝœ๋ฐฑํ•จ์ˆ˜๊ฐ€ ๋‹ค์‹œ ์‹คํ–‰ ๋จ
  • apply๋ฉ”์†Œ๋“œ๋ฅผ ์‚ฌ์šฉํ•ด์„œ this์˜ ๋ฒ”์œ„๋ฅผ ์ฝœ๋ฐฑํ•จ์ˆ˜๊ฐ€ ์‹คํ–‰๋  ๋•Œ ๊ทธ ์ปจํ…์ŠคํŠธ์˜ this๋กœ ๋งŒ๋“ฌ

 

 

๐Ÿ‘€์ฐธ๊ณ  (๊ฐ์‚ฌํ•ฉ๋‹ˆ๋‹ค)

๋”๋ณด๊ธฐ

http://yoonbumtae.com/?p=3584 

 

์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ: Debounce, Throttle ์ˆœ์ˆ˜ ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ (Vanilla JS)๋กœ ๊ตฌํ˜„ - BGSMM

์ฐธ๊ณ  ๊ธ€: ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ lodash: debounce์™€ throttle์„ ์ด์šฉํ•˜์—ฌ ํ•จ์ˆ˜์˜ ์‹คํ–‰ ํšŸ์ˆ˜ ์ œํ•œ   debounce: ๋™์ผ ์ด๋ฒคํŠธ๊ฐ€ ๋ฐ˜๋ณต์ ์œผ๋กœ ์‹œํ–‰๋˜๋Š” ๊ฒฝ์šฐ ๋งˆ์ง€๋ง‰ ์ด๋ฒคํŠธ๊ฐ€ ์‹คํ–‰๋˜๊ณ  ๋‚˜์„œ ์ผ์ • ์‹œ๊ฐ„(๋ฐ€๋ฆฌ์„ธ์ปจ๋“œ)๋™์•ˆ

yoonbumtae.com

 

https://velog.io/@qudgus21/%EB%94%94%EB%B0%94%EC%9A%B4%EC%8A%A4%EC%99%80-%EC%93%B0%EB%A1%9C%ED%8B%80%EB%A7%81

 

๋””๋ฐ”์šด์Šค์™€ ์“ฐ๋กœํ‹€๋ง

scroll, resize, input, mouseover ๋“ฑ์˜ ์ด๋ฒคํŠธ๋Š” ์งง์€ ์‹œ๊ฐ„์— ์—ฌ๋Ÿฌ ๋ฒˆ ๋ฐœ์ƒํ•˜๊ณ , ์ด๋Š” ์„ฑ๋Šฅ์ƒ์˜ ๋ฌธ์ œ๋ฅผ ์ผ์œผํ‚ฌ ์ˆ˜ ์žˆ๋‹ค. ๋””๋ฐ”์šด์Šค์™€ ์“ฐ๋กœํ‹€์€ ์งง์€ ์‹œ๊ฐ„ ๊ฐ„๊ฒฉ์œผ๋กœ ์—ฐ์†ํ•ด์„œ ๋ฐœ์ƒํ•˜๋Š” ์ด๋ฒคํŠธ๋ฅผ ๊ทธ๋ฃนํ™”ํ•ด

velog.io

 

https://pks2974.medium.com/throttle-%EC%99%80-debounce-%EA%B0%9C%EB%85%90-%EC%A0%95%EB%A6%AC%ED%95%98%EA%B8%B0-2335a9c426ff

 

Throttle ์™€ Debounce ๊ฐœ๋… ์ •๋ฆฌํ•˜๊ธฐ

Throttle ์™€ Debounce ๋ผ๋Š” ๊ฐœ๋… ์„ ์•Œ๊ฒŒ ๋˜์–ด ์ด๋ฅผ ์ •๋ฆฌํ•ด๋ณด๊ณ ์ž ํ•œ๋‹ค.

pks2974.medium.com

 

https://webclub.tistory.com/607

 

๋””๋ฐ”์šด์Šค(Debounce)์™€ ์Šค๋กœํ‹€(Throttle ) ๊ทธ๋ฆฌ๊ณ  ์ฐจ์ด์ 

Throttle, Debounce & Difference ์Šค๋กœํ‹€(Throttle) ๊ณผ ๋””๋ฐ”์šด์Šค(Debounce) ๋ž€ ๋ฌด์—‡์ผ๊นŒ? ์ด ๋‘ ๊ฐ€์ง€ ๋ฐฉ๋ฒ• ๋ชจ๋‘ DOM ์ด๋ฒคํŠธ๋ฅผ ๊ธฐ๋ฐ˜์œผ๋กœ ์‹คํ–‰ํ•˜๋Š” ์ž๋ฐ”์Šคํฌ๋ฆฝํŠธ๋ฅผ ์„ฑ๋Šฅ์ƒ์˜ ์ด์œ ๋กœ JS์˜ ์–‘์ ์ธ ์ธก๋ฉด, ์ฆ‰ ์ด๋ฒคํŠธ(ev.

webclub.tistory.com

 

https://medium.com/@kwoncharles/debounce%EC%99%80-throttle%EC%9D%98-%EC%B0%A8%EC%9D%B4-34845fbfa1ff

 

Debounce์™€ Throttle์˜ ์ฐจ์ด

๊ฐ„๋‹จํ•œ ์˜ˆ์ œ๋กœ ์•Œ์•„๋ณด๋Š” debounce์™€ throttle

medium.com

 

๋Œ“๊ธ€