Skip to content

Instantly share code, notes, and snippets.

@varfrog
Created February 2, 2026 11:51
Show Gist options
  • Select an option

  • Save varfrog/dbfa04bf92d2ccf7f61630d7d691348d to your computer and use it in GitHub Desktop.

Select an option

Save varfrog/dbfa04bf92d2ccf7f61630d7d691348d to your computer and use it in GitHub Desktop.
Collapse spaces - benchmark tests
package collapse
import (
"strings"
"testing"
)
var sink string // prevents compiler optimizations
func benchmarkInput() string {
return strings.Repeat("a b c d ", 200)
}
func BenchmarkCollapseContains(b *testing.B) {
input := benchmarkInput()
b.ResetTimer()
for i := 0; i < b.N; i++ {
sink = CollapseContains(input)
}
}
func BenchmarkCollapseBuilder(b *testing.B) {
input := benchmarkInput()
b.ResetTimer()
for i := 0; i < b.N; i++ {
sink = CollapseBuilder(input)
}
}
func BenchmarkCollapseFields(b *testing.B) {
input := benchmarkInput()
b.ResetTimer()
for i := 0; i < b.N; i++ {
sink = CollapseFields(input)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment