Skip to content

Commit e38d62a

Browse files
mscdexTrott
authored andcommitted
path: fix POSIX path.resolve() perf regression
PR-URL: #38064 Reviewed-By: Matteo Collina <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]>
1 parent cbe3b27 commit e38d62a

File tree

1 file changed

+19
-13
lines changed

1 file changed

+19
-13
lines changed

lib/path.js

+19-13
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323

2424
const {
2525
FunctionPrototypeBind,
26-
RegExp,
2726
StringPrototypeCharCodeAt,
2827
StringPrototypeIndexOf,
2928
StringPrototypeLastIndexOf,
@@ -48,6 +47,8 @@ const {
4847
validateString,
4948
} = require('internal/validators');
5049

50+
const platformIsWin32 = (process.platform === 'win32');
51+
5152
function isPathSeparator(code) {
5253
return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH;
5354
}
@@ -1011,24 +1012,29 @@ const win32 = {
10111012
posix: null
10121013
};
10131014

1015+
const posixCwd = (() => {
1016+
if (platformIsWin32) {
1017+
// Converts Windows' backslash path separators to POSIX forward slashes
1018+
// and truncates any drive indicator
1019+
const regexp = /\\/g;
1020+
return () => {
1021+
const cwd = StringPrototypeReplace(process.cwd(), regexp, '/');
1022+
return StringPrototypeSlice(cwd, StringPrototypeIndexOf(cwd, '/'));
1023+
};
1024+
}
1025+
1026+
// We're already on POSIX, no need for any transformations
1027+
return () => process.cwd();
1028+
})();
1029+
10141030
const posix = {
10151031
// path.resolve([from ...], to)
10161032
resolve(...args) {
10171033
let resolvedPath = '';
10181034
let resolvedAbsolute = false;
10191035

10201036
for (let i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) {
1021-
let path;
1022-
if (i >= 0) {
1023-
path = args[i];
1024-
} else {
1025-
const _ = StringPrototypeReplace(
1026-
process.cwd(),
1027-
new RegExp(`\\${module.exports.sep}`, 'g'),
1028-
posix.sep
1029-
);
1030-
path = StringPrototypeSlice(_, StringPrototypeIndexOf(_, posix.sep));
1031-
}
1037+
const path = i >= 0 ? args[i] : posixCwd();
10321038

10331039
validateString(path, 'path');
10341040

@@ -1431,4 +1437,4 @@ posix.posix = win32.posix = posix;
14311437
win32._makeLong = win32.toNamespacedPath;
14321438
posix._makeLong = posix.toNamespacedPath;
14331439

1434-
module.exports = process.platform === 'win32' ? win32 : posix;
1440+
module.exports = platformIsWin32 ? win32 : posix;

0 commit comments

Comments
 (0)