prefer-set-has
Added in v0.7.3Configuration
Rule Details
Prefer Set#has() over Array#includes() when checking for existence or
non-existence.
Set#has() is faster than Array#includes(). When an array variable binding is
used only for existence checks (and, optionally, a few other supported
references), this rule recommends declaring it as a Set instead. let and
var bindings qualify as well as const; a binding that is reassigned is
excluded by its own reference analysis, not by the declaration keyword.
Examples of incorrect code for this rule:
Examples of correct code for this rule:
An array with supported extra references can also be converted when it has more
than one includes() lookup. The array must be a plain literal with only
unique, statically known primitive or null values, and no holes, spreads, or
-0. Supported extra references are for…of, array spread, call or
constructor argument spread, .length, and .forEach() with a one-parameter
arrow function.
An array that is only checked once is left alone:
Options
Type: object
minimumItems
Type: integer
Default: 0
The minimum known array size before Set#has() is enforced. When this option
is greater than 0, the rule only reports arrays with a statically known size.
Examples of incorrect code for this rule with { "minimumItems": 5 }:
Examples of correct code for this rule with { "minimumItems": 5 }: