From 25fca063813e54416fedf167f262a9133fe152b7 Mon Sep 17 00:00:00 2001 From: gonzaloriestra <14979109+gonzaloriestra@users.noreply.github.com> Date: Sun, 7 Jun 2026 00:29:24 +0000 Subject: [PATCH] [Performance] Memoize isWsl Memoize the \`isWsl\` function in \`packages/cli-kit/src/public/node/system.ts\` to avoid redundant dynamic imports and filesystem/environment checks. This function is checked frequently during analytics generation. Performance Impact: Reduced dynamic imports and system probes after the first call. How to test your changes: CI --- .../cli-kit/src/public/node/system.test.ts | 12 ++++++++++++ packages/cli-kit/src/public/node/system.ts | 19 ++++++++++++++----- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/packages/cli-kit/src/public/node/system.test.ts b/packages/cli-kit/src/public/node/system.test.ts index ed12516ba4..cba9cea54f 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 firstCall = system.isWsl() + const secondCall = system.isWsl() + + // Then + expect(firstCall).toBe(secondCall) + await expect(firstCall).resolves.toBe(await secondCall) + }) +}) diff --git a/packages/cli-kit/src/public/node/system.ts b/packages/cli-kit/src/public/node/system.ts index b4f1e8cc69..facc7c1932 100644 --- a/packages/cli-kit/src/public/node/system.ts +++ b/packages/cli-kit/src/public/node/system.ts @@ -98,9 +98,9 @@ export interface CaptureOutputResult { * @example * ```typescript * const result = await captureOutputWithExitCode('ls', ['-la']) - * if (result.exitCode !== 0) \{ + * if (result.exitCode !== 0) { * console.error('Command failed:', result.stderr) - * \} + * } * ``` */ export async function captureOutputWithExitCode( @@ -351,14 +351,23 @@ 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 { + // Memoize the promise to avoid redundant dynamic imports and filesystem/environment checks. + // This is checked frequently during analytics generation. + return (memoizedIsWsl ??= (async () => { + const wsl = await import('is-wsl') + return wsl.default + })()) } /**