Prioritize reducing memory allocations and choosing efficient data structures to improve performance. Avoid unnecessary string/byte conversions, use maps for O(1) lookups instead of slices, and leverage compiler optimizations where possible.
Key strategies:
utils.EqualFold
instead of utils.ToLower
for case-insensitive comparisons to avoid allocationsmap[int]bool
instead of []int
for O(1) performancestrings.ToLower(scheme + "://" + host)
instead of separate calls[]byte
to string
comparisonsfasthttp.ParseFloat
and UnsafeBytes
when availableExample:
// Inefficient - multiple allocations
if utils.ToLower(cookie.SameSite) == CookieSameSiteNoneMode {
// process
}
// Efficient - no allocations
if utils.EqualFold(cookie.SameSite, CookieSameSiteNoneMode) {
// process
}
// Inefficient - O(n) lookup
var cacheableStatusCodes = []int{200, 203, 300, 301}
if slices.Contains(cacheableStatusCodes, status) { /* ... */ }
// Efficient - O(1) lookup
var cacheableStatusCodes = map[int]bool{200: true, 203: true, 300: true, 301: true}
if cacheableStatusCodes[status] { /* ... */ }
Enter the URL of a public GitHub repository