diff --git a/src/compiler/path.ts b/src/compiler/path.ts index b05216adc47b5..4d283266d9162 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; } @@ -684,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;