Debounce & Throttle Implementation in Go
14-day free trial
Try SeekerPro free for 14 days.
Full access while you try it · then $15.99/mo · cancel anytime
Start your 14-day free SeekerPro trial →30-day money-back guarantee · Cancel anytime
Not checking a site today? Turn this topic into a ready to paste AI prompt, free →
Key Takeaways
- ✓Debounce & Throttle runs in O(1) time and O(1) space, in Go as in every other language.
- ✓time.AfterFunc returns a *time.Timer whose Stop() cancels the pending call, which is a debounce in two lines.
- ✓Guard the timer with a mutex, because the debounced function is called from many goroutines by definition, and call Stop() on the previous timer before assigning the new one.
- ✓The Go trap: time.Tick (the convenience function, not NewTicker) has no Stop and its underlying Ticker is NEVER garbage collected, the standard library documentation says so outright.
- ✓Go timers are managed by the runtime in per-processor heaps, so arming and stopping are cheap and scale across cores without a global lock.
What Debounce & Throttle actually is
Debounce & Throttle in Go: what you actually reach for
Go implementation
package debounce
import (
"sync"
"time"
)
type Debouncer struct {
mu sync.Mutex
timer *time.Timer
wait time.Duration
}
func New(wait time.Duration) *Debouncer {
return &Debouncer{wait: wait}
}
func (d *Debouncer) Call(fn func()) {
d.mu.Lock()
defer d.mu.Unlock()
if d.timer != nil {
d.timer.Stop() // cancel the pending call, reset the window
}
d.timer = time.AfterFunc(d.wait, fn)
}
// Throttle: NewTicker + defer Stop. NEVER time.Tick, its Ticker is
// never garbage collected, so it leaks for the life of the process.
func Throttle(period time.Duration, done <-chan struct{}, fn func()) {
ticker := time.NewTicker(period)
defer ticker.Stop()
for {
select {
case <-ticker.C:
fn()
case <-done:
return
}
}
}Think Your Code Is Clean? Let NexusBro QA It in 20 Seconds.
Paste your code. Click QA. Get an instant expert-level audit with fixes.
QA My Code FreeThe Go-specific trap
Performance in Go, beyond the O(1)
Real-World Applications
Interview Tips & Practice
Frequently Asked Questions
Which Go type or library should I use for Debounce & Throttle?
time.AfterFunc returns a *time.Timer whose Stop() cancels the pending call, which is a debounce in two lines. time.NewTicker gives you a throttle. The whole timer API is in the standard library and needs no dependency.
What is the most common Go mistake when implementing Debounce & Throttle?
time.Tick (the convenience function, not NewTicker) has no Stop and its underlying Ticker is NEVER garbage collected, the standard library documentation says so outright. Using it in a request handler leaks a timer per request for the lifetime of the process. The related trap: Timer.Stop() returns false if the timer already fired, and for a hand-built time.NewTimer you must drain its channel before Reset or you will read a stale tick.
How does Debounce & Throttle actually perform in Go?
Go timers are managed by the runtime in per-processor heaps, so arming and stopping are cheap and scale across cores without a global lock. AfterFunc runs the callback on its own goroutine, so a slow callback does not delay other timers but does mean your debounced function needs to be safe to run concurrently with itself if it outlives the interval. Time complexity is O(1) and space is O(1).
Related Articles
Rather try before you buy?
Get the rest of the vault: every cheat sheet, guide and audit in SeekerPro, free for 14 days, then $15.99/mo. Cancel anytime. Daily usage limits apply.
Start my 14-day SeekerPro trialIf you are a student, see what Replit offers students for free on Noizz.
Is your site built to last?
Run a free QA audit and get your Site Health Score in seconds.
Check Your Site FreeNo signup required
Reader comments
No account needed. Share what worked, what did not, and what you would add.
Add your take below. A concrete tip, a result or a question helps the next reader most.