Skip to content

Commit

Permalink
path: simplify normalizeString
Browse files Browse the repository at this point in the history
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 <zyszys98@gmail.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
  • Loading branch information
BridgeAR authored and danbev committed Apr 18, 2019
1 parent 4f8b497 commit 9946c59
Showing 1 changed file with 7 additions and 7 deletions.
14 changes: 7 additions & 7 deletions lib/path.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,11 @@ function isWindowsDeviceRoot(code) {

// Resolves . and .. elements in a path with directory names
function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
var res = '';
var lastSegmentLength = 0;
var lastSlash = -1;
var dots = 0;
var code;
let res = '';
let lastSegmentLength = 0;
let lastSlash = -1;
let dots = 0;
let code = 0;
for (var i = 0; i <= path.length; ++i) {
if (i < path.length)
code = path.charCodeAt(i);
Expand All @@ -66,7 +66,7 @@ function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
if (isPathSeparator(code)) {
if (lastSlash === i - 1 || dots === 1) {
// NOOP
} else if (lastSlash !== i - 1 && dots === 2) {
} else if (dots === 2) {
if (res.length < 2 || lastSegmentLength !== 2 ||
res.charCodeAt(res.length - 1) !== CHAR_DOT ||
res.charCodeAt(res.length - 2) !== CHAR_DOT) {
Expand All @@ -82,7 +82,7 @@ function normalizeString(path, allowAboveRoot, separator, isPathSeparator) {
lastSlash = i;
dots = 0;
continue;
} else if (res.length === 2 || res.length === 1) {
} else if (res.length !== 0) {
res = '';
lastSegmentLength = 0;
lastSlash = i;
Expand Down

0 comments on commit 9946c59

Please sign in to comment.