module: CJS loader re-stats the same missing paths within one require tree
@maxday arbeitet bereits daran.
Seit 22.7.2026.
- Vorherrschende Sprache
- JavaScript
- Sterne
- 122k
- Forks
- 37.3k
- Ø Merge
- 4 T. 2 Std.
- Gemergte PRs (30 T.)
- 283
Beschreibung
The CommonJS loader keeps a per-require-tree statCache (in lib/internal/modules/cjs/loader.js) to avoid re-stat-ing the same path while resolving a module tree. Today it only caches successful stats. Negative results (e.g. -ENOENT) fall through the cache and are re-probed every time the same missing path is looked up again within the same top-level require.
// lib/internal/modules/cjs/loader.js — stat()
const result = internalFsBinding.internalModuleStat(filename);
if (statCache !== null && result >= 0) {
// Only set cache when `internalModuleStat(filename)` succeeds.
statCache.set(filename, result);
}
Failed probes are extremely common during resolution, and the same misses recur across sibling and descendant modules:
tryExtensionsresolves an extensionless specifier (e.g.require('./helper')) bystat-ing each candidate in order:./helper.js, then./helper.json, then./helper.nodeuntil one exists. Every extension tried before the real one is a miss.- Bare specifiers (
require('foo')) walk thenode_moduleschain upward:…/a/b/node_modules,…/a/node_modules,…/node_modules. Most of those parent directories don't exist, so each is a negative stat, and every bare require in the tree re-walks the same non-existent ancestors.
Because negatives aren't cached, these misses are re-stat-ed repeatedly within a single resolution pass.
Beitragsleitfaden
Erste Schritte
- Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
- Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
- Forke das Repository und arbeite in einem Branch.
- Öffne einen Pull Request, der die Issue-Nummer nennt.
Bewertung
Dieses Issue wurde noch nicht bewertet.