Skip to content

Commit a79fa45

Browse files
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.
1 parent 500372e commit a79fa45

File tree

2 files changed

+5
-3
lines changed

2 files changed

+5
-3
lines changed

packages/tailwindcss/src/utils/infer-data-type.bench.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import { inferDataType } from './infer-data-type'
33

44
const colors = [
55
'slateblue',
6+
'black',
7+
'orange',
68
'rgb(255, 255, 255)',
79
'rgba(255, 255, 255, 1)',
810
'hsl(0, 0%, 100%)',

packages/tailwindcss/src/utils/is-color.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const NAMED_COLORS = [
1+
const NAMED_COLORS = new Set([
22
// CSS Level 1 colors
33
'black',
44
'silver',
@@ -191,14 +191,14 @@ const NAMED_COLORS = [
191191
'graytext',
192192
'accentcolor',
193193
'accentcolortext',
194-
]
194+
])
195195

196196
const IS_COLOR_FN = /^(rgba?|hsla?|hwb|color|(ok)?(lab|lch)|light-dark|color-mix)\(/i
197197

198198
export function isColor(value: string): boolean {
199199
return (
200200
value.charCodeAt(0) === 35 /* "#" */ ||
201201
IS_COLOR_FN.test(value) ||
202-
NAMED_COLORS.includes(value.toLowerCase())
202+
NAMED_COLORS.has(value.toLowerCase())
203203
)
204204
}

0 commit comments

Comments
 (0)