Inefficient Membership Test (ARCH3075)
Esta página aún no está disponible en tu idioma.
Key typescript.performance.inefficient-membership-test · Severity warning · Category performance
A membership check via includes on an array is O(n); when repeated, especially inside a loop, it should be reduced to O(1) with a Set.
Incorrect
Section titled “Incorrect”function selectAllowed(items: string[], allowed: string[]): string[] { return items.filter((item) => allowed.includes(item));}Correct
Section titled “Correct”function selectAllowed(items: string[], allowed: string[]): string[] { const allowedSet = new Set(allowed); return items.filter((item) => allowedSet.has(item));}Convert the array to a Set and use has for repeated membership checks.