perf: optimize makeMap

This commit is contained in:
Evan You 2023-11-16 17:02:17 +08:00
parent 81e941da5b
commit ae6fba9495
1 changed files with 4 additions and 6 deletions

View File

@ -9,10 +9,8 @@ export function makeMap(
str: string, str: string,
expectsLowerCase?: boolean expectsLowerCase?: boolean
): (key: string) => boolean { ): (key: string) => boolean {
const map: Record<string, boolean> = Object.create(null) const set = new Set(str.split(','))
const list: Array<string> = str.split(',') return expectsLowerCase
for (let i = 0; i < list.length; i++) { ? val => set.has(val.toLowerCase())
map[list[i]] = true : val => set.has(val)
}
return expectsLowerCase ? val => !!map[val.toLowerCase()] : val => !!map[val]
} }