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

+7-7
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
function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
53-
var res = '';
54-
var lastSegmentLength = 0;
55-
var lastSlash = -1;
56-
var dots = 0;
57-
var code;
53+
let res = '';
54+
let lastSegmentLength = 0;
55+
let lastSlash = -1;
56+
let dots = 0;
57+
let code = 0;
5858
for (var i = 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-
} else if (lastSlash !== i - 1 && dots === 2) {
69+
} else if (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-
} else if (res.length === 2 || res.length === 1) {
85+
} else if (res.length !== 0) {
8686
res = '';
8787
lastSegmentLength = 0;
8888
lastSlash = i;

0 commit comments

Comments
 (0)