From 9589802119416ef12f75fc040ac395eab596cdc4 Mon Sep 17 00:00:00 2001 From: gonzaloriestra <14979109+gonzaloriestra@users.noreply.github.com> Date: Sat, 23 May 2026 00:24:32 +0000 Subject: [PATCH] [Performance] Memoize platformAndArch and use startsWith Memoize the result of `platformAndArch` when called with default arguments (the current process's platform and architecture) to avoid redundant computations. This function is called in hot paths like analytics reporting. Additionally, replaced the regex match for Windows platform detection with `startsWith('win')`, which is more efficient. Verified with unit tests and quality checks. --- packages/cli-kit/src/public/node/os.ts | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/packages/cli-kit/src/public/node/os.ts b/packages/cli-kit/src/public/node/os.ts index 139f798cbb0..7e9d032caa3 100644 --- a/packages/cli-kit/src/public/node/os.ts +++ b/packages/cli-kit/src/public/node/os.ts @@ -46,6 +46,12 @@ export async function username(platform: typeof process.platform = process.platf type PlatformArch = Exclude | 'amd64' | '386' type PlatformStrings = Exclude | 'windows' + +/** + * Memoized value for the platform and architecture. + */ +let memoizedPlatformAndArch: {platform: PlatformStrings; arch: PlatformArch} | undefined + /** * Returns the platform and architecture. * @returns Returns the current platform and architecture. @@ -57,6 +63,10 @@ export function platformAndArch( platform: PlatformStrings arch: PlatformArch } { + if (platform === process.platform && arch === process.arch && memoizedPlatformAndArch) { + return memoizedPlatformAndArch + } + let archString: PlatformArch if (arch === 'x64') { archString = 'amd64' @@ -65,8 +75,13 @@ export function platformAndArch( } else { archString = arch } - const platformString = (platform.match(/^win.+/) ? 'windows' : platform) as PlatformStrings - return {platform: platformString, arch: archString} + const platformString = (platform.startsWith('win') ? 'windows' : platform) as PlatformStrings + + const result = {platform: platformString, arch: archString} + if (platform === process.platform && arch === process.arch) { + memoizedPlatformAndArch = result + } + return result } function getEnvironmentVariable() {