Skip to content

setHasExistenceChecks

Prefer Set.has() over Array.includes() for repeated existence checks.

✅ This rule is included in the ts stylisticStrict presets.

When an array is only used for existence checks via .includes(), converting it to a Set and using .has() provides better performance. Array.includes() has O(n) time complexity, while Set.has() has O(1) time complexity.

This rule reports when:

  • A const variable is initialized with an array (literal, Array(), Array.from(), Array.of(), or array methods like .filter(), .map(), etc.)
  • All usages of that variable are .includes() calls with exactly one argument
  • Either there are multiple .includes() calls, or a single call is inside a loop or function
const items = [1, 2, 3];
function check(value: number) {
return items.includes(value);
}
const items = [1, 2, 3];
items.includes(1);
items.includes(2);
const items = [1, 2, 3];
for (const value of values) {
items.includes(value);
}

This rule is not configurable.

If the array is very small (a few elements), the performance difference between Array.includes() and Set.has() is negligible. You may also disable this rule if you need to preserve the array type for compatibility with APIs that expect arrays.

Made with ❤️‍🔥 in Boston by Josh Goldberg and contributors.