Skip to content

Commit

Permalink
perf: avoid unnecessary fs.statSync calls
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
marvinhagemeister committed Jan 10, 2023
1 parent 46de0e8 commit a89bdbd
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/mean-seals-tease.md
Original file line number Diff line number Diff line change
@@ -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.
21 changes: 20 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit a89bdbd

Please sign in to comment.