Skip to content

Commit 9946c59

Browse files
BridgeARdanbev
authored andcommitted
path: simplify normalizeString
This improves the `path.normalize()` and `path.resolve()` performance a tiny bit. One statement could never be truthy, another check could be simplified and `code` is now monomorphic. PR-URL: #27240 Reviewed-By: Yongsheng Zhang <[email protected]> Reviewed-By: Luigi Pinca <[email protected]>
1 parent 4f8b497 commit 9946c59

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

‎lib/path.js‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,11 +50,11 @@ function isWindowsDeviceRoot(code){
5050

5151
// Resolves . and .. elements in a path with directory names
5252
functionnormalizeString(path,allowAboveRoot,separator,isPathSeparator){
53-
varres='';
54-
varlastSegmentLength=0;
55-
varlastSlash=-1;
56-
vardots=0;
57-
varcode;
53+
letres='';
54+
letlastSegmentLength=0;
55+
letlastSlash=-1;
56+
letdots=0;
57+
letcode=0;
5858
for(vari=0;i<=path.length;++i){
5959
if(i<path.length)
6060
code=path.charCodeAt(i);
@@ -66,7 +66,7 @@ function normalizeString(path, allowAboveRoot, separator, isPathSeparator){
6666
if(isPathSeparator(code)){
6767
if(lastSlash===i-1||dots===1){
6868
// NOOP
69-
}elseif(lastSlash!==i-1&&dots===2){
69+
}elseif(dots===2){
7070
if(res.length<2||lastSegmentLength!==2||
7171
res.charCodeAt(res.length-1)!==CHAR_DOT||
7272
res.charCodeAt(res.length-2)!==CHAR_DOT){
@@ -82,7 +82,7 @@ function normalizeString(path, allowAboveRoot, separator, isPathSeparator){
8282
lastSlash=i;
8383
dots=0;
8484
continue;
85-
}elseif(res.length===2||res.length===1){
85+
}elseif(res.length!==0){
8686
res='';
8787
lastSegmentLength=0;
8888
lastSlash=i;

0 commit comments

Comments
(0)