名称: js-set-map-lookups 描述: 使用Set和Map进行O(1)成员查找,代替array.includes()。适用于重复检查成员资格或对集合进行频繁查找的情况。
使用Set/Map进行O(1)查找
将数组转换为Set/Map以进行重复的成员资格检查。
错误(每次检查O(n)):
const allowedIds = ['a', 'b', 'c', ...]
items.filter(item => allowedIds.includes(item.id))
正确(每次检查O(1)):
const allowedIds = new Set(['a', 'b', 'c', ...])
items.filter(item => allowedIds.has(item.id))