From 5838482a9b219455bc650484997f92cf21a594bd Mon Sep 17 00:00:00 2001 From: gonzaloriestra <14979109+gonzaloriestra@users.noreply.github.com> Date: Fri, 26 Jun 2026 00:18:02 +0000 Subject: [PATCH] [Performance] Memoize isWsl result The `isWsl` function performs a dynamic import and environment check every time it is called. Since the environment doesn't change during the CLI execution, memoizing the result improves performance and reduces overhead, especially during analytics reporting. --- packages/cli-kit/src/public/node/system.test.ts | 12 ++++++++++++ packages/cli-kit/src/public/node/system.ts | 13 ++++++++++--- 2 files changed, 22 insertions(+), 3 deletions(-) diff --git a/packages/cli-kit/src/public/node/system.test.ts b/packages/cli-kit/src/public/node/system.test.ts index ed12516ba40..fb0ba1c8350 100644 --- a/packages/cli-kit/src/public/node/system.test.ts +++ b/packages/cli-kit/src/public/node/system.test.ts @@ -393,3 +393,15 @@ describe('readStdinString', () => { await expect(got).rejects.toThrow('Stdin input exceeded the maximum allowed size.') }) }) + +describe('isWsl', () => { + test('memoizes the result', async () => { + // When + const result1 = system.isWsl() + const result2 = system.isWsl() + + // Then + expect(result1).toBe(result2) + await expect(result1).resolves.toBeDefined() + }) +}) diff --git a/packages/cli-kit/src/public/node/system.ts b/packages/cli-kit/src/public/node/system.ts index 3f449f8ff34..15d52142a1a 100644 --- a/packages/cli-kit/src/public/node/system.ts +++ b/packages/cli-kit/src/public/node/system.ts @@ -354,14 +354,21 @@ export function isCI(): boolean { return isTruthy(process.env.CI) } +/** + * Memoized value for the WSL check. + */ +let memoizedIsWsl: Promise | undefined + /** * Check if the current environment is a WSL environment. * * @returns True if the current environment is a WSL environment. */ -export async function isWsl(): Promise { - const wsl = await import('is-wsl') - return wsl.default +export function isWsl(): Promise { + return (memoizedIsWsl ??= (async () => { + const wsl = await import('is-wsl') + return wsl.default + })()) } /**