diff --git a/bytesconv.go b/bytesconv.go index b0bc54c..0e1b83c 100644 --- a/bytesconv.go +++ b/bytesconv.go @@ -297,17 +297,10 @@ func hexChar(c byte) byte { // EqualBytesStr returns true if string(b) == s. // -// It doesn't allocate memory unlike string(b) do. +// This function has no performance benefits comparing to string(b) == s. +// It is left here for backwards compatilbility only. func EqualBytesStr(b []byte, s string) bool { - if len(s) != len(b) { - return false - } - for i, n := 0, len(s); i < n; i++ { - if s[i] != b[i] { - return false - } - } - return true + return string(b) == s } // AppendBytesStr appends src to dst and returns dst diff --git a/bytesconv_timing_test.go b/bytesconv_timing_test.go index 2dc2a8b..688b021 100644 --- a/bytesconv_timing_test.go +++ b/bytesconv_timing_test.go @@ -36,3 +36,15 @@ func BenchmarkLowercaseBytesMixed(b *testing.B) { } }) } + +func BenchmarkEqualBytesStr(b *testing.B) { + s := "foobarbaraz" + bs := []byte(s) + b.RunParallel(func(pb *testing.PB) { + for pb.Next() { + if !EqualBytesStr(bs, s) { + b.Fatalf("unexpected result: %q != %q", bs, s) + } + } + }) +}