From 14343bead80095497bb6694fba8b9bf20ee25765 Mon Sep 17 00:00:00 2001 From: Andrew Casey Date: Fri, 21 May 2021 10:28:22 -0700 Subject: [PATCH] Avoid calling replace in normalizeSlashes when it would do nothing (#44100) * Make normalizeSlashes a no-op there are no bad slashes On Windows, there will probably be a negligible slowdown, iterating over the pre-slash prefix of each unnormalized path (though we might come out ahead if paths are normalized more than once). On *nix, this saves work - 1.8s -> 0.4s in the project I'm investigating. * Reuse already-computed index --- src/compiler/path.ts | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/compiler/path.ts b/src/compiler/path.ts index 911398a07a0..27b5330b2ce 100644 --- a/src/compiler/path.ts +++ b/src/compiler/path.ts @@ -452,6 +452,11 @@ namespace ts { * Normalize path separators, converting `\` into `/`. */ export function normalizeSlashes(path: string): string { + const index = path.indexOf("\\"); + if (index === -1) { + return path; + } + backslashRegExp.lastIndex = index; // prime regex with known position return path.replace(backslashRegExp, directorySeparator); }