From 73043ce8d0289055c34101422bd9005f3e8d2d9d Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Tue, 29 Oct 2024 21:24:58 +0000 Subject: [PATCH 1/2] Try adding a non-allocating path to `getPathFromPathComponents`. --- src/compiler/path.ts | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/compiler/path.ts b/src/compiler/path.ts index b05216adc47b5..32fa57ec80679 100644 --- a/src/compiler/path.ts +++ b/src/compiler/path.ts @@ -511,8 +511,14 @@ export function getPathComponents(path: string, currentDirectory = "") { */ export function getPathFromPathComponents(pathComponents: readonly T[], length?: number): T { if (pathComponents.length === 0) return "" as T; + const firstComponent = pathComponents[0]; + + // Fast path to just join everything together without slicing the array. + if (pathComponents.length > 1 && firstComponent.length > 0 && !hasTrailingDirectorySeparator(firstComponent)) { + return pathComponents.join(directorySeparator) as T; + } - const root = pathComponents[0] && ensureTrailingDirectorySeparator(pathComponents[0]); + const root = firstComponent && ensureTrailingDirectorySeparator(firstComponent); return root + pathComponents.slice(1, length).join(directorySeparator) as T; } From bb2ca84549bf298943e24b2b105334d309756d97 Mon Sep 17 00:00:00 2001 From: Daniel Rosenwasser Date: Tue, 29 Oct 2024 21:25:23 +0000 Subject: [PATCH 2/2] Drive-by replacement of `substr` with `slice`. --- src/compiler/path.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/compiler/path.ts b/src/compiler/path.ts index 32fa57ec80679..4d283266d9162 100644 --- a/src/compiler/path.ts +++ b/src/compiler/path.ts @@ -690,7 +690,7 @@ export function removeTrailingDirectorySeparator(path: string): string; /** @internal */ export function removeTrailingDirectorySeparator(path: string) { if (hasTrailingDirectorySeparator(path)) { - return path.substr(0, path.length - 1); + return path.slice(0, -1); } return path;