From a92e602a70ab6ccd9053328f8b4a41ae3b121d8e Mon Sep 17 00:00:00 2001 From: gonzaloriestra <14979109+gonzaloriestra@users.noreply.github.com> Date: Fri, 22 May 2026 00:53:33 +0000 Subject: [PATCH] [Performance] Memoize isShopify environment check Memoize the result promise of isShopify when using the default process.env. This avoids redundant filesystem checks for the /opt/dev/bin/dev executable, which is particularly beneficial in hot paths like analytics reporting. The cache is bypassed during unit tests to maintain isolation. --- .../cli-kit/src/public/node/context/local.ts | 25 ++++++++++++++++--- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/packages/cli-kit/src/public/node/context/local.ts b/packages/cli-kit/src/public/node/context/local.ts index 2337585075..efa821a10e 100644 --- a/packages/cli-kit/src/public/node/context/local.ts +++ b/packages/cli-kit/src/public/node/context/local.ts @@ -54,6 +54,11 @@ export function isVerbose(env = process.env): boolean { return isTruthy(env[environmentVariables.verbose]) || process.argv.includes('--verbose') } +/** + * Memoized value for the Shopify environment check. + */ +let memoizedIsShopify: Promise | undefined + /** * Returns true if the environment in which the CLI is running is either * a local environment (where dev is present). @@ -62,11 +67,23 @@ export function isVerbose(env = process.env): boolean { * @returns True if the CLI is used in a Shopify environment. */ export async function isShopify(env = process.env): Promise { - if (Object.prototype.hasOwnProperty.call(env, environmentVariables.runAsUser)) { - return !isTruthy(env[environmentVariables.runAsUser]) + if (env === process.env && memoizedIsShopify !== undefined && !isUnitTest()) { + return memoizedIsShopify + } + + const resultPromise = (async () => { + if (Object.prototype.hasOwnProperty.call(env, environmentVariables.runAsUser)) { + return !isTruthy(env[environmentVariables.runAsUser]) + } + const devInstalled = await lazyFileExists(pathConstants.executables.dev) + return devInstalled + })() + + if (env === process.env && !isUnitTest()) { + memoizedIsShopify = resultPromise } - const devInstalled = await lazyFileExists(pathConstants.executables.dev) - return devInstalled + + return resultPromise } /**