Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try avoid slice-ing in getPathFromPathComponents. #60371

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/compiler/path.ts
Original file line number Diff line number Diff line change
Expand Up @@ -511,8 +511,14 @@ export function getPathComponents(path: string, currentDirectory = "") {
*/
export function getPathFromPathComponents<T extends string>(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;
}

Expand Down Expand Up @@ -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;
Expand Down