Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Avoid unnecessary byte/string conversion
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) { reg, _ := regexp.Compile("[^0-9]") for i := 0; i < b.N; i++ { if match := reg.Match([]byte("v")); !match { b.Fail() } } } func BenchmarkMatchString(b *testing.B) { reg, _ := regexp.Compile("[^0-9]") for i := 0; i < b.N; i++ { if match := reg.MatchString("v"); !match { b.Fail() } } } BenchmarkMatch-16 19712776 65.47 ns/op 1 B/op 1 allocs/op BenchmarkMatchString-16 24261463 51.16 ns/op 0 B/op 0 allocs/op Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
- Loading branch information