Skip to content

Commit 92f68e3

Browse files
committedAug 14, 2020
perf: cache normalized paths
1 parent 0c6b218 commit 92f68e3

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed
 

‎src/lib/utils/path.ts

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
import * as nodePath from 'path';
22

3+
const PATH_REGEXP = new RegExp('\\' + nodePath.win32.sep, 'g');
4+
const ensureUnixPathCache = new Map<string, string>();
5+
36
export const ensureUnixPath = (path?: string): string | null => {
47
if (!path) {
58
return null;
69
}
710

11+
const cachePath = ensureUnixPathCache.get(path);
12+
if (cachePath) {
13+
return cachePath;
14+
}
15+
816
// we use a regex instead of the character literal due to a bug in some versions of node.js
917
// the path separator needs to be preceded by an escape character
10-
const regex = new RegExp('\\' + nodePath.win32.sep, 'g');
11-
return path.replace(regex, nodePath.posix.sep);
18+
const normalizedPath = path.replace(PATH_REGEXP, nodePath.posix.sep);
19+
ensureUnixPathCache.set(path, normalizedPath);
20+
return normalizedPath;
1221
};

0 commit comments

Comments
 (0)