diff --git a/packages/cli-kit/src/public/node/context/local.ts b/packages/cli-kit/src/public/node/context/local.ts index 2388db690d..4ec8161bc8 100644 --- a/packages/cli-kit/src/public/node/context/local.ts +++ b/packages/cli-kit/src/public/node/context/local.ts @@ -34,6 +34,16 @@ export function homeDirectory(): string { return homedir() } +/** + * Memoized value for the verbose check. + */ +let memoizedIsVerbose: boolean | undefined + +/** + * Memoized value for the unit test check. + */ +let memoizedIsUnitTest: boolean | undefined + /** * Returns true if the CLI is running in debug mode. * @@ -51,6 +61,11 @@ export function isDevelopment(env = process.env): boolean { * @returns True if SHOPIFY_FLAG_VERBOSE is truthy or the flag --verbose has been passed. */ export function isVerbose(env = process.env): boolean { + if (env === process.env) { + // Memoize the result to avoid repeated scans of process.argv and env lookups + // in high-frequency paths like outputDebug. + return (memoizedIsVerbose ??= isTruthy(env[environmentVariables.verbose]) || process.argv.includes('--verbose')) + } return isTruthy(env[environmentVariables.verbose]) || process.argv.includes('--verbose') } @@ -88,6 +103,11 @@ export async function isShopify(env = process.env): Promise { * @returns True if the SHOPIFY_UNIT_TEST environment variable is truthy. */ export function isUnitTest(env = process.env): boolean { + if (env === process.env) { + // Memoize the result as SHOPIFY_UNIT_TEST is static during execution + // and checked frequently to suppress output. + return (memoizedIsUnitTest ??= isTruthy(env[environmentVariables.unitTest])) + } return isTruthy(env[environmentVariables.unitTest]) }