From a79fa45bf2ac57854d41a3d0c6f226090944c0c5 Mon Sep 17 00:00:00 2001 From: Jordan Pittman Date: Fri, 29 Mar 2024 09:15:09 -0400 Subject: [PATCH] Make `isColor` faster (#13404) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- packages/tailwindcss/src/utils/infer-data-type.bench.ts | 2 ++ packages/tailwindcss/src/utils/is-color.ts | 6 +++--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/packages/tailwindcss/src/utils/infer-data-type.bench.ts b/packages/tailwindcss/src/utils/infer-data-type.bench.ts index 82fe874bd..da44ca9fd 100644 --- a/packages/tailwindcss/src/utils/infer-data-type.bench.ts +++ b/packages/tailwindcss/src/utils/infer-data-type.bench.ts @@ -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%)', diff --git a/packages/tailwindcss/src/utils/is-color.ts b/packages/tailwindcss/src/utils/is-color.ts index 0448556db..f6abbf259 100644 --- a/packages/tailwindcss/src/utils/is-color.ts +++ b/packages/tailwindcss/src/utils/is-color.ts @@ -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()) ) }