Skip to content

Files

Latest commit

 

History

History
25 lines (18 loc) · 611 Bytes

prefer-set-has.md

File metadata and controls

25 lines (18 loc) · 611 Bytes

Pattern: Array#includes() for repeated lookups

Issue: -

Description

When performing frequent existence checks, using Set#has() is more performant than Array#includes(). If an array is primarily used for lookup operations, consider converting it to a Set.

Examples

Example of incorrect code:

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

Example of correct code:

const set = new Set([1, 2, 3]);
const hasValue = (value) => set.has(value);

// Single lookup is fine with array
const array = [1, 2, 3];
const hasOne = array.includes(1);