Skip to content

Latest commit

 

History

History
34 lines (25 loc) · 1.09 KB

prefer-set-has.md

File metadata and controls

34 lines (25 loc) · 1.09 KB

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

This rule is part of the recommended config.

🔧💡 This rule is auto-fixable and provides 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);