GitHub - floatdrop/debounce: A zero-allocation debouncer (original) (raw)

Debounce

Mentioned in Awesome Go CI Coverage Go Report Card Go Reference License: MIT

A simple, thread-safe debounce library for Go that delays function execution until after a specified duration has elapsed since the last invocation. Perfect for rate limiting, reducing redundant operations, and optimizing performance in high-frequency scenarios.

Features

Installation

go get github.com/floatdrop/debounce/v2

Usage

import ( "fmt" "time"

"github.com/floatdrop/debounce/v2"

)

func main() { debouncer := debounce.New(debounce.WithDelay(200 * time.Millisecond)) debouncer.Do(func() { fmt.Println("Hello") }) debouncer.Do(func() { fmt.Println("World") }) time.Sleep(time.Second) // Output: World }

Benchmarks

go test -bench=. -benchmem

goos: darwin
goarch: arm64
pkg: github.com/floatdrop/debounce/v2
cpu: Apple M3 Max
BenchmarkDebounce_Insert-14    	 4860848	       234.0 ns/op	       0 B/op	       0 allocs/op
BenchmarkDebounce_Do-14        	 5188065	       230.8 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/floatdrop/debounce/v2	7.805s

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

License

This project is licensed under the MIT License - see the LICENSE file for details.