There is some code like above. In this case, it is a little bit inappropriate to use the regular expression. This regular expression is used to determine whether the url starts with https scheme. Here, a simple HasPrefix is fine
I write a benchmark to show this.
var httpsRegexp = regexp.MustCompile(`^https:\/\/`)
// BenchmarkMatch-12 11180230 99.44 ns/op
func BenchmarkMatch(b *testing.B) {
b.Run("use-regex", func(b *testing.B) {
for i := 0; i < b.N; i++ {
httpsRegexp.MatchString("https://abc.abc.abc")
}
})
b.Run("use-prefix", func(b *testing.B) {
for i := 0; i < b.N; i++ {
strings.HasPrefix("https://abc.abc.abc", "https")
}
})
}
// BenchmarkMatch
// BenchmarkMatch/use-regex
// BenchmarkMatch/use-regex-12 11810976 100.2 ns/op
// BenchmarkMatch/use-prefix
// BenchmarkMatch/use-prefix-12 347787476 3.451 ns/op
// PASS
There is some code like above. In this case, it is a little bit inappropriate to use the regular expression. This regular expression is used to determine whether the url starts with https scheme. Here, a simple
HasPrefix
is fineI write a benchmark to show this.
It seems better.