Skip to content
← Back to rules

unicorn/prefer-set-has 性能

An auto-fix is available for this rule.

它的作用

在检查存在性或非存在性时,优先使用 Set#has() 而不是 Array#includes()

为什么这是个问题?

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

参考资料