setHasExistenceChecks
Prefer
Set.has()overArray.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
constvariable 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
Examples
Section titled “Examples”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);}const items = new Set([1, 2, 3]);function check(value: number) { return items.has(value);}const items = new Set([1, 2, 3]);items.has(1);items.has(2);// Single call, not in loop/function - acceptableconst items = [1, 2, 3];items.includes(1);// Has other usages besides .includes()const items = [1, 2, 3];items.push(4);items.includes(1);// Exported arrays are not reportedexport const items = [1, 2, 3];items.includes(1);items.includes(2);Options
Section titled “Options”This rule is not configurable.
When Not To Use It
Section titled “When Not To Use It”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.
Further Reading
Section titled “Further Reading”Equivalents in Other Linters
Section titled “Equivalents in Other Linters”- ESLint:
unicorn/prefer-set-has - Oxlint:
unicorn/prefer-set-has
Made with ❤️🔥 in Boston by
Josh Goldberg and contributors.