From 4cf89280c7446b396b67c43900b297b6b3e0907a Mon Sep 17 00:00:00 2001 From: Keith Cirkel Date: Thu, 9 May 2019 18:39:50 +0100 Subject: [PATCH] [Refactor] check existence of `node_modules` This offers a small but useful performance improvement by avoiding unnecessary stat calls. --- lib/async.js | 10 ++++++++-- test/mock.js | 2 ++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/lib/async.js b/lib/async.js index cd25a6b4..b1127942 100644 --- a/lib/async.js +++ b/lib/async.js @@ -233,8 +233,14 @@ module.exports = function resolve(x, options, callback) { if (dirs.length === 0) return cb(null, undefined); var dir = dirs[0]; - var file = path.join(dir, x); - loadAsFile(file, opts.package, onfile); + isDirectory(dir, isdir); + + function isdir(err, isdir) { + if (err) return cb(err); + if (!isdir) return processDirs(cb, dirs.slice(1)); + var file = path.join(dir, x); + loadAsFile(file, opts.package, onfile); + } function onfile(err, m, pkg) { if (err) return cb(err); diff --git a/test/mock.js b/test/mock.js index fe6bcf3d..d4f57a31 100644 --- a/test/mock.js +++ b/test/mock.js @@ -108,6 +108,7 @@ test('mock package', function (t) { var dirs = {}; dirs[path.resolve('/foo')] = true; + dirs[path.resolve('/foo/node_modules')] = true; function opts(basedir) { return { @@ -142,6 +143,7 @@ test('mock package from package', function (t) { var dirs = {}; dirs[path.resolve('/foo')] = true; + dirs[path.resolve('/foo/node_modules')] = true; function opts(basedir) { return {