This is an issue when using Array#includes() to check if a value exists in an array that is used primarily for existence checking and is checked multiple times.

Why is this an issue?

Using Array#includes() for existence checks can lead to performance problems, especially with larger datasets.

Array#includes() has O(n) time complexity because it needs to iterate through the array elements until it finds a match or reaches the end. In contrast, Set#has() has O(1) average time complexity because it uses a hash table internally.

For small arrays (typically under 10-20 elements), the performance difference is negligible. However, as the array size grows, the performance gap becomes significant. For an array with 1000 elements, Array#includes() might need to check up to 1000 elements, while Set#has() performs the check almost instantly.

This performance difference becomes even more pronounced when the existence check is performed multiple times, such as in loops or frequently called functions.

What is the potential impact?

Performance degradation can occur when checking existence in large arrays, especially when these checks are performed frequently. This can lead to slower application response times and poor user experience in performance-critical code paths.

How to fix?

Replace the array with a Set when the primary use case is existence checking. Convert the array to a Set using the Set constructor, then use Set#has() instead of Array#includes().

Non-compliant code example

const allowedValues = [1, 2, 3, 4, 5];
const isAllowed = value => allowedValues.includes(value); // Noncompliant

Compliant code example

const allowedValues = new Set([1, 2, 3, 4, 5]);
const isAllowed = value => allowedValues.has(value);

Documentation