Skip to content

floatdrop/debounce

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

52 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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

  • Zero allocations: No allocations on sunbsequent debounce calls
  • Thread-safe: Safe for concurrent use across multiple goroutines
  • Channel support: Can be used on top of chan with Chan function.
  • Configurable delays and limits: Set custom behaviour with WithDelay and WithLimit options
  • Zero dependencies: Built using only Go standard library

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.

About

A zero-allocation debouncer

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • Go 100.0%