Skip to content

Latest commit

Β 

History

History
37 lines (28 loc) Β· 1.29 KB

File metadata and controls

37 lines (28 loc) Β· 1.29 KB

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

πŸ’Ό This rule is enabled in the following configs: βœ… recommended, β˜‘οΈ unopinionated.

πŸ”§πŸ’‘ This rule is automatically fixable by the --fix CLI option and manually fixable by editor suggestions.

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

Examples

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

// βœ…
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);