From 0f0944b56d182cf3e577325045190c0291c4e125 Mon Sep 17 00:00:00 2001 From: gonzaloriestra <14979109+gonzaloriestra@users.noreply.github.com> Date: Wed, 20 May 2026 13:39:06 +0000 Subject: [PATCH] [Performance] Memoize isVerbose and isUnitTest in local context Memoize the result of `isVerbose` and `isUnitTest` when called with `process.env`. These checks are performed frequently in hot paths like logging (`outputDebug`), and memoizing them avoids repeated `process.argv.includes` scans and redundant environment variable lookups. The memoization explicitly bypasses the cache when a custom environment object is provided, ensuring test isolation remains intact. Performance impact: - Reduces overhead in high-frequency logging paths. - Avoids O(N) array scans for the `--verbose` flag on every debug log. --- .../cli-kit/src/public/node/context/local.ts | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) 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]) }