We can use `(*regexp.Regexp).MatchString` instead of
`(*regexp.Regexp).Match([]byte(...))` to avoid unnecessary `[]byte`
conversions and reduce allocations.
Example benchmark:
func BenchmarkMatch(b *testing.B) {
for i := 0; i < b.N; i++ {
if match := themeColorRegex.Match([]byte("N1")); !match {
b.Fail()
}
}
}
func BenchmarkMatchString(b *testing.B) {
for i := 0; i < b.N; i++ {
if match := themeColorRegex.MatchString("N1"); !match {
b.Fail()
}
}
}
goos: linux
goarch: amd64
pkg: oss.terrastruct.com/d2/lib/color
cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics
BenchmarkMatch-16 9894165 114.3 ns/op 2 B/op 1 allocs/op
BenchmarkMatchString-16 13439838 83.61 ns/op 0 B/op 0 allocs/op
PASS
ok oss.terrastruct.com/d2/lib/color 3.306s
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
|
||
|---|---|---|
| .. | ||
| color.go | ||