unicorn/prefer-set-has
eslint-plugin-unicorn
Safe autofix
eslint.config.js
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-set-has": "error",
},
},
];Prefer Set#has() over Array#includes() when checking for existence or non-existence.
const array = [1, 2, 3];
const hasValue = value => array.includes(value);const array = [1, 2, 3];
for (const item of array) {
console.log(item);
}
const length = array.length;
const hasValue = value => array.includes(value);const set = new Set([1, 2, 3]);
const hasValue = value => set.has(value);const set = new Set([1, 2, 3]);
for (const item of set) {
console.log(item);
}
const length = set.size;
const hasValue = value => set.has(value);// This array has a usage that does not work the same on a `Set`.
const array = [1, 2];
const hasValue = value => array.includes(value);
array.push(3);Examples from the ESLint documentation.
unicorn/prefer-set-has
eslint-plugin-unicorn
import unicorn from "eslint-plugin-unicorn";
export default [
{
plugins: { unicorn },
rules: {
"unicorn/prefer-set-has": "error",
},
},
];unicorn/prefer-set-has
unicorn plugin · perf
{
"rules": {
"unicorn/prefer-set-has": "error"
}
}Biome does not have this rule.
Prefer Response.json() over new Response(JSON.stringify()).
Enforce combining multiple Array#{push,unshift}(), Element#classList.{add,remove}(), and importScripts() into one call.
Prefer Reflect.apply() over Function#apply().
Prefer String#replaceAll() over regex searches with the global flag and String#split().join().
Prefer borrowing methods from the prototype instead of the instance.
Prefer using structuredClone to create a deep clone.