AgentSkillsCN

js-set-map-lookups

在反复检查成员资格时,优先使用 Set 和 Map 实现 O(1) 的成员查找,而非依赖 array.includes()。适用于频繁检查成员资格,或对集合进行多次查找的场景。

SKILL.md
--- frontmatter
name: js-set-map-lookups
description: Use Set and Map for O(1) membership lookups instead of array.includes(). Apply when checking membership repeatedly or performing frequent lookups against a collection.

Use Set/Map for O(1) Lookups

Convert arrays to Set/Map for repeated membership checks.

Incorrect (O(n) per check):

typescript
const allowedIds = ['a', 'b', 'c', ...]
items.filter(item => allowedIds.includes(item.id))

Correct (O(1) per check):

typescript
const allowedIds = new Set(['a', 'b', 'c', ...])
items.filter(item => allowedIds.has(item.id))