Aller au contenu

Inefficient Membership Test (ARCH3075)

Ce contenu n’est pas encore disponible dans votre langue.

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.

function selectAllowed(items: string[], allowed: string[]): string[] {
return items.filter((item) => allowed.includes(item));
}
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.