Skip to content

Latest commit

 

History

History
32 lines (24 loc) · 789 Bytes

prefer-set-has.md

File metadata and controls

32 lines (24 loc) · 789 Bytes

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

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

This rule is fixable.

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);