Skip to content
← Back to rules

unicorn/prefer-set-has パフォーマンス

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

何をするか

存在または非存在の確認において、Array#includes() の代わりに Set#has() を推奨します。

なぜ問題なのか

Set#has()Array#includes() よりも高速です。

このルールに違反する 誤った 例:

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

このルールに準拠する 正しい 例:

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);

使い方

設定ファイルまたは CLI でこのルールを 有効化 するには、以下のように使用できます:

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

参照