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‎

Lines changed: 19 additions & 13 deletions
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+
constplatformIsWin32=(process.platform==='win32');
51+
5152
functionisPathSeparator(code){
5253
returncode===CHAR_FORWARD_SLASH||code===CHAR_BACKWARD_SLASH;
5354
}
@@ -1011,24 +1012,29 @@ const win32 ={
10111012
posix: null
10121013
};
10131014

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

10201036
for(leti=args.length-1;i>=-1&&!resolvedAbsolute;i--){
1021-
letpath;
1022-
if(i>=0){
1023-
path=args[i];
1024-
}else{
1025-
const_=StringPrototypeReplace(
1026-
process.cwd(),
1027-
newRegExp(`\\${module.exports.sep}`,'g'),
1028-
posix.sep
1029-
);
1030-
path=StringPrototypeSlice(_,StringPrototypeIndexOf(_,posix.sep));
1031-
}
1037+
constpath=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)