Ir al contenido

Repeated Work In Loop (ARCH5072)

Esta página aún no está disponible en tu idioma.

Key go.performance.repeated-work-in-loop · Severity info · Category performance

A computation whose inputs do not depend on the loop variable re-runs on every iteration. Hoist loop-invariant expressions out of the loop.

func match(items []string, prefix string) []string {
var out []string
for _, item := range items {
if strings.HasPrefix(item, strings.ToLower(prefix)) {
out = append(out, item)
}
}
return out
}
func match(items []string, prefix string) []string {
lower := strings.ToLower(prefix)
var out []string
for _, item := range items {
if strings.HasPrefix(item, lower) {
out = append(out, item)
}
}
return out
}

Move computations whose inputs don’t depend on the loop variable before the loop.