unicorn/prefer-set-has Perf
它的作用
在检查某个值是否存在或不存在时,优先使用 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);如何使用
To enable this rule using the config file or in the CLI, you can use:
json
{
"rules": {
"unicorn/prefer-set-has": "error"
}
}ts
import { defineConfig } from "oxlint";
export default defineConfig({
rules: {
"unicorn/prefer-set-has": "error",
},
});bash
oxlint --deny unicorn/prefer-set-has版本
此规则是在 v0.13.2 中添加的。
