GeezerVison is showing github.com — path: improve path.resolve() performance when used as process.cwd() · nodejs/node@b882148
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: benchmark/path/join-posix.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,8 +16,8 @@ function main({ n, paths }) { | 16 | 16 |
| 17 | 17 | bench.start(); | 18 | 18 | for (let i = 0; i < n; i++) { | 19 | | - if (i % 3 === 0) { | 20 | | - copy[1] = `${orig}${i}`; | | 19 | + if (i % 5 === 0) { | | 20 | + copy[1] = `${orig}/${i}`; | 21 | 21 | posix.join(...copy); | 22 | 22 | } else { | 23 | 23 | posix.join(...args); | |
Collapse file Copy file name to clipboardExpand all lines: benchmark/path/join-win32.js +2-2Lines changed: 2 additions & 2 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -16,8 +16,8 @@ function main({ n, paths }) { | 16 | 16 |
| 17 | 17 | bench.start(); | 18 | 18 | for (let i = 0; i < n; i++) { | 19 | | - if (i % 3 === 0) { | 20 | | - copy[1] = `${orig}${i}`; | | 19 | + if (i % 5 === 0) { | | 20 | + copy[1] = `${orig}\\${i}`; | 21 | 21 | win32.join(...copy); | 22 | 22 | } else { | 23 | 23 | win32.join(...args); | |
Collapse file Copy file name to clipboardExpand all lines: benchmark/path/normalize-posix.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -17,7 +17,7 @@ const bench = common.createBenchmark(main, { | 17 | 17 | function main({ n, path }) { | 18 | 18 | bench.start(); | 19 | 19 | for (let i = 0; i < n; i++) { | 20 | | - posix.normalize(i % 3 === 0 ? `${path}${i}` : path); | | 20 | + posix.normalize(i % 5 === 0 ? `${path}/${i}` : path); | 21 | 21 | } | 22 | 22 | bench.end(n); | 23 | 23 | } |
Collapse file Copy file name to clipboardExpand all lines: benchmark/path/normalize-win32.js +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -17,7 +17,7 @@ const bench = common.createBenchmark(main, { | 17 | 17 | function main({ n, path }) { | 18 | 18 | bench.start(); | 19 | 19 | for (let i = 0; i < n; i++) { | 20 | | - win32.normalize(i % 3 === 0 ? `${path}${i}` : path); | | 20 | + win32.normalize(i % 5 === 0 ? `${path}\\${i}` : path); | 21 | 21 | } | 22 | 22 | bench.end(n); | 23 | 23 | } |
Collapse file Copy file name to clipboardExpand all lines: benchmark/path/resolve-posix.js +7-4Lines changed: 7 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,23 +4,26 @@ const { posix } = require('path'); | 4 | 4 |
| 5 | 5 | const bench = common.createBenchmark(main, { | 6 | 6 | paths: [ | | 7 | + 'empty', | 7 | 8 | '', | | 9 | + '.', | 8 | 10 | ['', ''].join('|'), | 9 | 11 | ['foo/bar', '/tmp/file/', '..', 'a/../subfile'].join('|'), | 10 | 12 | ['a/b/c/', '../../..'].join('|'), | | 13 | + ['/a/b/c/', 'abc'].join('|'), | 11 | 14 | ], | 12 | 15 | n: [1e5], | 13 | 16 | }); | 14 | 17 |
| 15 | 18 | function main({ n, paths }) { | 16 | | - const args = paths.split('|'); | | 19 | + const args = paths === 'empty' ? [] : paths.split('|'); | 17 | 20 | const copy = [...args]; | 18 | | - const orig = copy[0]; | | 21 | + const orig = copy[0] ?? ''; | 19 | 22 |
| 20 | 23 | bench.start(); | 21 | 24 | for (let i = 0; i < n; i++) { | 22 | | - if (i % 3 === 0) { | 23 | | - copy[0] = `${orig}${i}`; | | 25 | + if (i % 5 === 0) { | | 26 | + copy[0] = `${orig}/${i}`; | 24 | 27 | posix.resolve(...copy); | 25 | 28 | } else { | 26 | 29 | posix.resolve(...args); | |
Collapse file Copy file name to clipboardExpand all lines: benchmark/path/resolve-win32.js +6-4Lines changed: 6 additions & 4 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -4,7 +4,9 @@ const { win32 } = require('path'); | 4 | 4 |
| 5 | 5 | const bench = common.createBenchmark(main, { | 6 | 6 | paths: [ | | 7 | + 'empty', | 7 | 8 | '', | | 9 | + '.', | 8 | 10 | ['', ''].join('|'), | 9 | 11 | ['c:/ignore', 'd:\\a/b\\c/d', '\\e.exe'].join('|'), | 10 | 12 | ['c:/blah\\blah', 'd:/games', 'c:../a'].join('|'), | @@ -13,14 +15,14 @@ const bench = common.createBenchmark(main, { | 13 | 15 | }); | 14 | 16 |
| 15 | 17 | function main({ n, paths }) { | 16 | | - const args = paths.split('|'); | | 18 | + const args = paths === 'empty' ? [] : paths.split('|'); | 17 | 19 | const copy = [...args]; | 18 | | - const orig = copy[0]; | | 20 | + const orig = copy[0] ?? ''; | 19 | 21 |
| 20 | 22 | bench.start(); | 21 | 23 | for (let i = 0; i < n; i++) { | 22 | | - if (i % 3 === 0) { | 23 | | - copy[0] = `${orig}${i}`; | | 24 | + if (i % 5 === 0) { | | 25 | + copy[0] = `${orig}\\${i}`; | 24 | 26 | win32.resolve(...copy); | 25 | 27 | } else { | 26 | 28 | win32.resolve(...args); | |
Collapse file Copy file name to clipboardExpand all lines: lib/path.js +17-1Lines changed: 17 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -95,7 +95,7 @@ function normalizeString(path, allowAboveRoot, separator, isPathSeparator) { | 95 | 95 | StringPrototypeCharCodeAt(res, res.length - 1) !== CHAR_DOT || | 96 | 96 | StringPrototypeCharCodeAt(res, res.length - 2) !== CHAR_DOT) { | 97 | 97 | if (res.length > 2) { | 98 | | - const lastSlashIndex = StringPrototypeLastIndexOf(res, separator); | | 98 | + const lastSlashIndex = res.length - lastSegmentLength - 1; | 99 | 99 | if (lastSlashIndex === -1) { | 100 | 100 | res = ''; | 101 | 101 | lastSegmentLength = 0; | @@ -163,6 +163,8 @@ function _format(sep, pathObject) { | 163 | 163 | return dir === pathObject.root ? `${dir}${base}` : `${dir}${sep}${base}`; | 164 | 164 | } | 165 | 165 |
| | 166 | +const forwardSlashRegExp = /\//g; | | 167 | + | 166 | 168 | const win32 = { | 167 | 169 | /** | 168 | 170 | * path.resolve([from ...], to) | @@ -186,6 +188,14 @@ const win32 = { | 186 | 188 | } | 187 | 189 | } else if (resolvedDevice.length === 0) { | 188 | 190 | path = process.cwd(); | | 191 | + // Fast path for current directory | | 192 | + if (args.length === 0 || ((args.length === 1 && (args[0] === '' || args[0] === '.')) && | | 193 | + isPathSeparator(StringPrototypeCharCodeAt(path, 0)))) { | | 194 | + if (!isWindows) { | | 195 | + path = StringPrototypeReplace(path, forwardSlashRegExp, '\\'); | | 196 | + } | | 197 | + return path; | | 198 | + } | 189 | 199 | } else { | 190 | 200 | // Windows has the concept of drive-specific current working | 191 | 201 | // directories. If we've resolved a drive letter but not yet an | @@ -1171,6 +1181,12 @@ const posix = { | 1171 | 1181 | * @returns {string} | 1172 | 1182 | */ | 1173 | 1183 | resolve(...args) { | | 1184 | + if (args.length === 0 || (args.length === 1 && (args[0] === '' || args[0] === '.'))) { | | 1185 | + const cwd = posixCwd(); | | 1186 | + if (StringPrototypeCharCodeAt(cwd, 0) === CHAR_FORWARD_SLASH) { | | 1187 | + return cwd; | | 1188 | + } | | 1189 | + } | 1174 | 1190 | let resolvedPath = ''; | 1175 | 1191 | let resolvedAbsolute = false; | 1176 | 1192 |
| |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-path-resolve.js +4Lines changed: 4 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -24,6 +24,8 @@ const resolveTests = [ | 24 | 24 | [['c:/ignore', 'd:\\a/b\\c/d', '\\e.exe'], 'd:\\e.exe'], | 25 | 25 | [['c:/ignore', 'c:/some/file'], 'c:\\some\\file'], | 26 | 26 | [['d:/ignore', 'd:some/dir//'], 'd:\\ignore\\some\\dir'], | | 27 | + [[], process.cwd()], | | 28 | + [[''], process.cwd()], | 27 | 29 | [['.'], process.cwd()], | 28 | 30 | [['//server/share', '..', 'relative\\'], '\\\\server\\share\\relative'], | 29 | 31 | [['c:/', '//'], 'c:\\'], | @@ -42,6 +44,8 @@ const resolveTests = [ | 42 | 44 | [[['/var/lib', '../', 'file/'], '/var/file'], | 43 | 45 | [['/var/lib', '/../', 'file/'], '/file'], | 44 | 46 | [['a/b/c/', '../../..'], posixyCwd], | | 47 | + [[], posixyCwd], | | 48 | + [[''], posixyCwd], | 45 | 49 | [['.'], posixyCwd], | 46 | 50 | [['/some/dir', '.', '/absolute/'], '/absolute'], | 47 | 51 | [['/foo/tmp.3/', '../tmp.3/cycles/root.js'], '/foo/tmp.3/cycles/root.js'], | |
|