From 1037fd54e05203dc53cc3cdbd0fdb6360d9f2cf2 Mon Sep 17 00:00:00 2001 From: gonzaloriestra <14979109+gonzaloriestra@users.noreply.github.com> Date: Thu, 21 May 2026 00:49:00 +0000 Subject: [PATCH] [Performance] Memoize shouldDisplayColors Memoize the result of `shouldDisplayColors` when called with the default `process` to avoid redundant environment lookups and TTY checks in high-frequency logging paths. --- packages/cli-kit/src/public/node/output.ts | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/packages/cli-kit/src/public/node/output.ts b/packages/cli-kit/src/public/node/output.ts index d1488702da1..9e058d529be 100644 --- a/packages/cli-kit/src/public/node/output.ts +++ b/packages/cli-kit/src/public/node/output.ts @@ -229,6 +229,11 @@ function shouldOutput(logLevel: LogLevel): boolean { // eslint-disable-next-line import-x/no-mutable-exports export let collectedLogs: Record = {} +/** + * Memoized value for the color check. + */ +let memoizedShouldDisplayColors: boolean | undefined + /** * This is only used during UnitTesting. * If we are in a testing context, instead of printing the logs to the console, @@ -410,12 +415,18 @@ export function unstyled(message: string): string { * @returns True if the console outputs should display colors, false otherwise. */ export function shouldDisplayColors(_process = process): boolean { + if (_process === process && memoizedShouldDisplayColors !== undefined) { + return memoizedShouldDisplayColors + } + const {env, stdout} = _process - if (Object.hasOwnProperty.call(env, 'FORCE_COLOR')) { - return isTruthy(env.FORCE_COLOR) - } else { - return Boolean(stdout.isTTY) + const result = Object.hasOwnProperty.call(env, 'FORCE_COLOR') ? isTruthy(env.FORCE_COLOR) : Boolean(stdout.isTTY) + + if (_process === process) { + memoizedShouldDisplayColors = result } + + return result } /**