---
title: "Discussion on \"Debouncing vs Throttling: Handling Expensive Function Calls in JavaScript\" | Hashnode"
description: "Discussion on \"Debouncing vs Throttling: Handling Expensive Function Calls in JavaScript\". Learn the key differences between debouncing and throttling in JavaScript to optimize function execution and improve web app performance"
url: "https://hashnode.com/posts/debouncing-vs-throttling-handling-expensive-function-calls-in-javascript/67f0c6eceaeb17d0195a291d"
---

[Feed](https://hashnode.com/feed) /

Discussion

# [Debouncing vs Throttling: Handling Expensive Function Calls in JavaScript](https://fantom0.hashnode.dev/debouncing-vs-throttling-handling-expensive-function-calls-in-javascript?utm_source=hashnode&utm_medium=feed)

Learn the key differences between debouncing and throttling in JavaScript to optimize function execution and improve web app performance

Apr 5, 2025 · 4 min read · fantom0.hashnode.dev  
Read the article

-   [#debouncing](https://hashnode.com/tag/debouncing)
-   [#debouncing-and-throttling](https://hashnode.com/tag/debouncing-and-throttling)
-   [#throttling](https://hashnode.com/tag/throttling)
-   [#throttling-in-javascript](https://hashnode.com/tag/throttling-in-javascript)
-   [#debounce](https://hashnode.com/tag/debounce)
-   [#throttle](https://hashnode.com/tag/throttle)
-   [#javascript](https://hashnode.com/tag/javascript)
-   [#javascript-framework](https://hashnode.com/tag/javascript-framework)
-   [#vanilla-js-1](https://hashnode.com/tag/vanilla-js-1)
-   [#vanilla-js](https://hashnode.com/tag/vanilla-js)
-   [#nuanced](https://hashnode.com/tag/nuanced)
-   [#webdev](https://hashnode.com/tag/webdev)
-   [#web-development](https://hashnode.com/tag/web-development)

## Written by

[Peter Bamigboye](https://hashnode.com/@Fantom0)

@Fantom0

  
[View profile](https://hashnode.com/@Fantom0)

Student Of Tech

I am Peter, a front-end web developer who writes on current technologies that I'm learning or technologies that I feel the need to simplify.

## Responses 1

Markdown supported · ⌘↵ to post

[Olumide Olubosede](https://hashnode.com/@LordOnaka)

[Apr 5, 2025](https://hashnode.com/posts/debouncing-vs-throttling-handling-expensive-function-calls-in-javascript/67f0c6eceaeb17d0195a291d/comment/67f110dc3f9b1c50b722efd5)

The timer is defined inside the function. Doesn't that mean that a new timer is created every time the function is run?

[Peter Bamigboye](https://hashnode.com/@Fantom0)

[Apr 6, 2025](https://hashnode.com/posts/debouncing-vs-throttling-handling-expensive-function-calls-in-javascript/67f0c6eceaeb17d0195a291d/comment/67f110dc3f9b1c50b722efd5#reply-67f2110446dc4c254d0026fb)

hmm, well that's a good question

let's see, does a new timer get created each time? Yes, setTimeout creates a new timer every time the debounced function is called. But here's the important part:

timer is not re-declared on every call.

It persists because it's stored in a closure—defined in the outer function (debounce) but used in the returned inner function.

This means that:

Only one timer variable exists per debounced function and every call to the returned function clears the existing timer using clearTimeout(timer) before setting a new one.

That’s what ensures only the last call results in fn being executed—after the delay.

Why this works:

JavaScript functions have closures, so when the inner function (the one you attach to the event listener) uses timer, it’s always referring to the same variable in memory, not a new one each time.

---

Now if we had declared let timer inside the returned function like this:

return function (...args) { let timer = setTimeout(() => fn.apply(this, args), delay); };

Then a new timer variable would be created each time, and clearTimeout(timer) would have no effect—because it would only cancel the newly-created timer, not the previous ones.

That would break the debounce logic.

---

Also, why didn't we declare timer even outside the whole debounce function?

Well, that's cause we want to have encapsulation, basically we want to ensure that debounce function can maintain an independent behaviour

Using global variables is usually also discouraged cause you might mistakenly redeclare it down the line and cause bugs

So yeah, basically for the sake of maintaining independent behaviour and avoiding potential bugs.
