Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

Commit

Permalink
perf(httpClient): compare strings with strings.EqualFold (#962)
Browse files Browse the repository at this point in the history
Comparing two strings to the same case with `strings.ToLower` is more
computational expensive than `strings.EqualFold`.

Sample benchmark:

func BenchmarkToLower(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if strings.ToLower("CONTENT-TYPE") != strings.ToLower("content-type") {
			b.Fail()
		}
	}
}

func BenchmarkEqualFold(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if !strings.EqualFold("CONTENT-TYPE", "content-type") {
			b.Fail()
		}
	}
}

goos: linux
goarch: amd64
pkg: github.com/datreeio/datree/pkg/httpClient
cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics
BenchmarkToLower-16      	 8183317	       192.1 ns/op	      16 B/op	       1 allocs/op
BenchmarkEqualFold-16    	82634701	        12.92 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	github.com/datreeio/datree/pkg/httpClient	4.181s

Reference: https://staticcheck.dev/docs/checks/#SA6005

Signed-off-by: Eng Zer Jun <[email protected]>
  • Loading branch information
Juneezee authored Jul 24, 2023
1 parent 305e848 commit 0b67300
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/httpClient/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ func (c *Client) parseBody(body interface{}) (io.ReadWriter, error) {

func (c *Client) getValueOfHeader(headers map[string]string, header string) string {
for currentHeader, currentValue := range headers {
if strings.ToLower(currentHeader) == strings.ToLower(header) {
if strings.EqualFold(currentHeader, header) {
return currentValue
}
}
Expand Down

0 comments on commit 0b67300

Please sign in to comment.