Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@apocelipes
Copy link
Contributor

What does this do?

Use errors.New for non-formatted strings.

errors.New performs better, and since fmt.Errorf ultimately calls errors.New if there is no error needs be wrapped, we can use errors.New directly.

No functionality changes.

Notes

Benchmarks:

func BenchmarkFmtErrorf(b *testing.B) {
	for b.Loop() {
		_ = fmt.Errorf("gocron: DailyJob: atTimes minutes and seconds must be between 0 and 59 inclusive")
	}
}

func BenchmarkErrorsNew(b *testing.B) {
	for b.Loop() {
		_ = errors.New("gocron: DailyJob: atTimes minutes and seconds must be between 0 and 59 inclusive")
	}
}
image

errors.New is about 6 times faster and uses less memory.

Copy link
Contributor

@JohnRoesler JohnRoesler left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the detailed PR description!

@JohnRoesler JohnRoesler merged commit b383ca9 into go-co-op:v2 Sep 2, 2025
3 checks passed
@apocelipes apocelipes deleted the use-errors-new branch September 3, 2025 03:38
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.

2 participants