Skip to content

unicorn/prefer-set-has Perf

⚠️🛠️️ A dangerous auto-fix is available for this rule.

What it does

Prefer Set#has() over Array#includes() when checking for existence or non-existence.

Why is this bad?

Set#has() is faster than Array#includes().

Examples

Examples of incorrect code for this rule:

js
const array = [1, 2, 3];
const hasValue = (value) => array.includes(value);

Examples of correct code for this rule:

js
const set = new Set([1, 2, 3]);
const hasValue = (value) => set.has(value);
js
const array = [1, 2, 3];
const hasOne = array.includes(1);

How to use

To enable this rule in the CLI or using the config file, you can use:

bash
oxlint --deny unicorn/prefer-set-has
json
{
  "rules": {
    "unicorn/prefer-set-has": "error"
  }
}

References

Released under the MIT License.