From a89bdbd19b3b15739e7cc39fefa7506e55c3606b Mon Sep 17 00:00:00 2001 From: Marvin Hagemeister Date: Tue, 10 Jan 2023 15:18:46 +0100 Subject: [PATCH] perf: avoid unnecessary fs.statSync calls Only resolve a module dir when we now that the mapped path is a directory. This improves linting time by about 18% in projects with many files. --- .changeset/mean-seals-tease.md | 5 +++++ src/index.ts | 21 ++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) create mode 100644 .changeset/mean-seals-tease.md diff --git a/.changeset/mean-seals-tease.md b/.changeset/mean-seals-tease.md new file mode 100644 index 00000000..173c1c2f --- /dev/null +++ b/.changeset/mean-seals-tease.md @@ -0,0 +1,5 @@ +--- +'eslint-import-resolver-typescript': patch +--- + +Only try to resolve a module directory when we know that the path is a directory. This can lead to a 15% speedup on projects with many files. diff --git a/src/index.ts b/src/index.ts index b7656f96..54d17cc9 100644 --- a/src/index.ts +++ b/src/index.ts @@ -287,7 +287,26 @@ function getMappedPath( ]), ) .flat(2) - .filter(mappedPath => isFile(mappedPath) || isModule(mappedPath)) + .filter(mappedPath => { + if (mappedPath === undefined) { + return false + } + + try { + const stat = fs.statSync(mappedPath, { throwIfNoEntry: false }) + if (stat === undefined) return false + if (stat.isFile()) return true + + // Maybe this is a module dir? + if (stat.isDirectory()) { + return isModule(mappedPath) + } + } catch { + return false + } + + return false + }) } if (retry && paths.length === 0) {