X Tutup
The Wayback Machine - https://web.archive.org/web/20220420052242/https://github.com/nodejs/node/commit/e38d62a8c9
Skip to content
Permalink
Browse files
path: fix POSIX path.resolve() perf regression
PR-URL: #38064
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
  • Loading branch information
mscdex authored and Trott committed Apr 6, 2021
1 parent cbe3b27 commit e38d62a8c9e3494a464c0de6218178329f245440
Showing with 19 additions and 13 deletions.
  1. +19 −13 lib/path.js
@@ -23,7 +23,6 @@

const {
FunctionPrototypeBind,
RegExp,
StringPrototypeCharCodeAt,
StringPrototypeIndexOf,
StringPrototypeLastIndexOf,
@@ -48,6 +47,8 @@ const {
validateString,
} = require('internal/validators');

const platformIsWin32 = (process.platform === 'win32');

function isPathSeparator(code) {
return code === CHAR_FORWARD_SLASH || code === CHAR_BACKWARD_SLASH;
}
@@ -1011,24 +1012,29 @@ const win32 = {
posix: null
};

const posixCwd = (() => {
if (platformIsWin32) {
// Converts Windows' backslash path separators to POSIX forward slashes
// and truncates any drive indicator
const regexp = /\\/g;
return () => {
const cwd = StringPrototypeReplace(process.cwd(), regexp, '/');
return StringPrototypeSlice(cwd, StringPrototypeIndexOf(cwd, '/'));
};
}

// We're already on POSIX, no need for any transformations
return () => process.cwd();
})();

const posix = {
// path.resolve([from ...], to)
resolve(...args) {
let resolvedPath = '';
let resolvedAbsolute = false;

for (let i = args.length - 1; i >= -1 && !resolvedAbsolute; i--) {
let path;
if (i >= 0) {
path = args[i];
} else {
const _ = StringPrototypeReplace(
process.cwd(),
new RegExp(`\\${module.exports.sep}`, 'g'),
posix.sep
);
path = StringPrototypeSlice(_, StringPrototypeIndexOf(_, posix.sep));
}
const path = i >= 0 ? args[i] : posixCwd();

validateString(path, 'path');

@@ -1431,4 +1437,4 @@ posix.posix = win32.posix = posix;
win32._makeLong = win32.toNamespacedPath;
posix._makeLong = posix.toNamespacedPath;

module.exports = process.platform === 'win32' ? win32 : posix;
module.exports = platformIsWin32 ? win32 : posix;

0 comments on commit e38d62a

Please sign in to comment.
X Tutup