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
This commit is contained in:
Andrew Casey 2021-05-21 10:28:22 -07:00 committed by GitHub
parent 086423729a
commit 14343bead8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -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);
}