تخطَّ إلى المحتوى

Inefficient Membership Test (ARCH3075)

هذا المحتوى غير متوفر بلغتك بعد.

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.