Make isColor faster (#13404)

When the arrays of colors were split, both smaller, and had different values to lookup an `includes` check was faster. Since they’ve been merged a Set is now beneficial.
This commit is contained in:
Jordan Pittman 2024-03-29 09:15:09 -04:00 • committed by GitHub
parent 500372e728
commit a79fa45bf2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 5 additions and 3 deletions

View file

@ -3,6 +3,8 @@ import { inferDataType } from './infer-data-type'
const colors = [
'slateblue',
'black',
'orange',
'rgb(255, 255, 255)',
'rgba(255, 255, 255, 1)',
'hsl(0, 0%, 100%)',

View file

@ -1,4 +1,4 @@
const NAMED_COLORS = [
const NAMED_COLORS = new Set([
// CSS Level 1 colors
'black',
'silver',
@ -191,7 +191,7 @@ const NAMED_COLORS = [
'graytext',
'accentcolor',
'accentcolortext',
]
])
const IS_COLOR_FN = /^(rgba?|hsla?|hwb|color|(ok)?(lab|lch)|light-dark|color-mix)\(/i
@ -199,6 +199,6 @@ export function isColor(value: string): boolean {
return (
value.charCodeAt(0) === 35 /* "#" */ ||
IS_COLOR_FN.test(value) ||
NAMED_COLORS.includes(value.toLowerCase())
NAMED_COLORS.has(value.toLowerCase())
)
}