From 7954f0c94799983d3b531bfb21f74d21725cfb3a Mon Sep 17 00:00:00 2001 From: Andrew Casey Date: Mon, 24 May 2021 09:38:44 -0700 Subject: [PATCH] Kick out of normalizePath if there's nothing to do (#44173) * Kick out of normalizePath if there's nothing to do ...using `relativePathSegmentRegExp`. Bonus: use a regex to handle "/./" to avoid splitting and joining in a common case. When building the compiler, for example, it looks like ~95% of arguments to `normalizePath` do not require any normalization. * Check normalization before and after . cleanup * Also cleanup leading ./ --- src/compiler/path.ts | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/compiler/path.ts b/src/compiler/path.ts index 27b5330b2ce..d09108d34c7 100644 --- a/src/compiler/path.ts +++ b/src/compiler/path.ts @@ -552,6 +552,19 @@ namespace ts { export function normalizePath(path: string): string { path = normalizeSlashes(path); + // Most paths don't require normalization + if (!relativePathSegmentRegExp.test(path)) { + return path; + } + // Some paths only require cleanup of `/./` or leading `./` + const simplified = path.replace(/\/\.\//g, "/").replace(/^\.\//, ""); + if (simplified !== path) { + path = simplified; + if (!relativePathSegmentRegExp.test(path)) { + return path; + } + } + // Other paths require full normalization const normalized = getPathFromPathComponents(reducePathComponents(getPathComponents(path))); return normalized && hasTrailingDirectorySeparator(path) ? ensureTrailingDirectorySeparator(normalized) : normalized; }