Skip to content

Commit bee7ee0

Browse files
committed
sys: Use readdir withFileTypes option to skip lots of stat syscalls
This makes walking large directory trees much more efficient on Node 10.10 or later. See: https://lwn.net/Articles/606995/ https://www.python.org/dev/peps/pep-0471/ nodejs/node#22020 https://nodejs.org/en/blog/release/v10.10.0/ Signed-off-by: Anders Kaseorg <[email protected]>
1 parent f84fd30 commit bee7ee0

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

src/compiler/sys.ts

+19-9
Original file line numberDiff line numberDiff line change
@@ -1353,23 +1353,32 @@ namespace ts {
13531353
function getAccessibleFileSystemEntries(path: string): FileSystemEntries {
13541354
perfLogger.logEvent("ReadDir: " + (path || "."));
13551355
try {
1356-
const entries = _fs.readdirSync(path || ".").sort();
1356+
const entries = _fs.readdirSync(path || ".", { withFileTypes: true });
13571357
const files: string[] = [];
13581358
const directories: string[] = [];
1359-
for (const entry of entries) {
1359+
for (const dirent of entries) {
1360+
// withFileTypes is not supported before Node 10.10.
1361+
const entry = typeof dirent === "string" ? dirent : dirent.name;
1362+
13601363
// This is necessary because on some file system node fails to exclude
13611364
// "." and "..". See https://github.com/nodejs/node/issues/4002
13621365
if (entry === "." || entry === "..") {
13631366
continue;
13641367
}
1365-
const name = combinePaths(path, entry);
13661368

13671369
let stat: any;
1368-
try {
1369-
stat = _fs.statSync(name);
1370+
if (typeof dirent === "string" || dirent.isSymbolicLink()) {
1371+
const name = combinePaths(path, entry);
1372+
1373+
try {
1374+
stat = _fs.statSync(name);
1375+
}
1376+
catch (e) {
1377+
continue;
1378+
}
13701379
}
1371-
catch (e) {
1372-
continue;
1380+
else {
1381+
stat = dirent;
13731382
}
13741383

13751384
if (stat.isFile()) {
@@ -1379,6 +1388,8 @@ namespace ts {
13791388
directories.push(entry);
13801389
}
13811390
}
1391+
files.sort();
1392+
directories.sort();
13821393
return { files, directories };
13831394
}
13841395
catch (e) {
@@ -1413,8 +1424,7 @@ namespace ts {
14131424
}
14141425

14151426
function getDirectories(path: string): string[] {
1416-
perfLogger.logEvent("ReadDir: " + path);
1417-
return filter<string>(_fs.readdirSync(path), dir => fileSystemEntryExists(combinePaths(path, dir), FileSystemEntryKind.Directory));
1427+
return getAccessibleFileSystemEntries(path).directories.slice();
14181428
}
14191429

14201430
function realpath(path: string): string {

0 commit comments

Comments
 (0)