Skip to content

Commit c231130

Browse files
bnoordhuisevanlucas
authored andcommitted
module: skip directories known not to exist
There is no point in trying to search for files in a directory that we know does not exist, so stop doing that. Reduces the total number of stat(2) calls and the number of stat(2) misses on a medium-sized application by about 21% and 29% respectively. Reduces the total number of package.json open(2) calls and the number of open(2) misses by about 21% and 93% (!) respectively. Before: % time seconds usecs/call calls errors syscall ------ ----------- ----------- --------- --------- ---------------- 50.93 0.178419 38 4702 lstat 29.08 0.101875 36 2800 2010 stat 11.36 0.039796 43 932 215 open 5.39 0.018897 34 550 fstat 3.24 0.011337 34 336 pread ------ ----------- ----------- --------- --------- ---------------- 100.00 0.350324 9320 2225 total After: % time seconds usecs/call calls errors syscall ------ ----------- ----------- --------- --------- ---------------- 55.49 0.176638 38 4702 lstat 24.76 0.078826 35 2225 1435 stat 10.19 0.032434 44 733 16 open 6.19 0.019719 36 550 fstat 3.37 0.010723 32 336 pread ------ ----------- ----------- --------- --------- ---------------- 100.00 0.318340 8546 1451 total PR-URL: #9196 Reviewed-By: Brian White <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Evan Lucas <[email protected]> Reviewed-By: Michaël Zasso <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 21ba3e3 commit c231130

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

lib/module.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ Module._findPath = function(request, paths, isMain) {
171171
var basePath = path.resolve(curPath, request);
172172
var filename;
173173

174+
const rc = stat(basePath);
174175
if (!trailingSlash) {
175-
const rc = stat(basePath);
176176
if (rc === 0) { // File.
177177
if (preserveSymlinks && !isMain) {
178178
filename = path.resolve(basePath);
@@ -193,13 +193,13 @@ Module._findPath = function(request, paths, isMain) {
193193
}
194194
}
195195

196-
if (!filename) {
196+
if (!filename && rc === 1) { // Directory.
197197
if (exts === undefined)
198198
exts = Object.keys(Module._extensions);
199199
filename = tryPackage(basePath, exts, isMain);
200200
}
201201

202-
if (!filename) {
202+
if (!filename && rc === 1) { // Directory.
203203
// try it with each of the extensions at "index"
204204
if (exts === undefined)
205205
exts = Object.keys(Module._extensions);

0 commit comments

Comments
 (0)