Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

auth/rules: compare strings with strings.EqualFold #2047

Merged
merged 1 commit into from
Jul 26, 2023
Merged

auth/rules: compare strings with strings.EqualFold #2047

merged 1 commit into from
Jul 26, 2023

Conversation

Juneezee
Copy link
Contributor

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("FOOBAR") != strings.ToLower("foobar") {
			b.Fail()
		}
	}
}

func BenchmarkEqualFold(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if !strings.EqualFold("FOOBAR", "foobar") {
			b.Fail()
		}
	}
}

Result:

goos: linux
goarch: amd64
pkg: micro.dev/v4/util/auth/rules
cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics
BenchmarkToLower-16      	22834522	        99.96 ns/op	       8 B/op	       1 allocs/op
BenchmarkEqualFold-16    	151317182	         8.163 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	micro.dev/v4/util/auth/rules	4.104s

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("FOOBAR") != strings.ToLower("foobar") {
			b.Fail()
		}
	}
}

func BenchmarkEqualFold(b *testing.B) {
	for i := 0; i < b.N; i++ {
		if !strings.EqualFold("FOOBAR", "foobar") {
			b.Fail()
		}
	}
}

goos: linux
goarch: amd64
pkg: micro.dev/v4/util/auth/rules
cpu: AMD Ryzen 7 PRO 4750U with Radeon Graphics
BenchmarkToLower-16      	22834522	        99.96 ns/op	       8 B/op	       1 allocs/op
BenchmarkEqualFold-16    	151317182	         8.163 ns/op	       0 B/op	       0 allocs/op
PASS
ok  	micro.dev/v4/util/auth/rules	4.104s

Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
@asim asim merged commit c34cb41 into micro:master Jul 26, 2023
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants