Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

path: remove unnecessary string copies #14438

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -788,7 +788,9 @@ const win32 = {
}
}
} else if (code === 47/*/*/ || code === 92/*\*/) {
return path[0];
// `path` contains just a path separator, exit early to avoid
// unnecessary work
return path;
}

for (var i = len - 1; i >= offset; --i) {
Expand Down Expand Up @@ -1040,7 +1042,7 @@ const win32 = {
if (len === 3) {
// `path` contains just a drive root, exit early to avoid
// unnecessary work
ret.root = ret.dir = path.slice(0, 3);
ret.root = ret.dir = path;
return ret;
}
isAbsolute = true;
Expand All @@ -1049,15 +1051,15 @@ const win32 = {
} else {
// `path` contains just a drive root, exit early to avoid
// unnecessary work
ret.root = ret.dir = path.slice(0, 2);
ret.root = ret.dir = path;
return ret;
}
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The

       ret.root = ret.dir = path[0];

below can be simplified to

       ret.root = ret.dir = path;

as well, AFAICT.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anyway could you add a comment // `(len <= 1)`

}
} else if (code === 47/*/*/ || code === 92/*\*/) {
// `path` contains just a path separator, exit early to avoid
// unnecessary work
ret.root = ret.dir = path[0];
ret.root = ret.dir = path;
return ret;
}

Expand Down
6 changes: 5 additions & 1 deletion test/parallel/test-path-parse-format.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ const winPaths = [
'file',
'.\\file',
'C:\\',
'C:',
'\\',
'',

// unc
Expand All @@ -42,7 +44,9 @@ const winPaths = [
];

const winSpecialCaseParseTests = [
['/foo/bar', { root: '/' }]
['/foo/bar', { root: '/' }],
['C:', { root: 'C:', dir: 'C:', base: '' }],
['C:\\', { root: 'C:\\', dir: 'C:\\', base: '' }]
];

const winSpecialCaseFormatTests = [
Expand Down