Skip to content

Latest commit

 

History

History
37 lines (27 loc) · 1.26 KB

prefer-set-has.md

File metadata and controls

37 lines (27 loc) · 1.26 KB

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

💼 This rule is enabled in the ✅ recommended config.

🔧💡 This rule is automatically fixable by the --fix CLI option and manually fixable by editor suggestions.

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

Fail

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

Pass

const set = new Set([1, 2, 3]);
const hasValue = value => set.has(value);
// This array is not only checking existence.
const array = [1, 2];
const hasValue = value => array.includes(value);
array.push(3);
// This array is only checked once.
const array = [1, 2, 3];
const hasOne = array.includes(1);