Skip to content

Commit

Permalink
[Refactor] check existence of node_modules
Browse files Browse the repository at this point in the history
This offers a small but useful performance improvement by avoiding
unnecessary stat calls.
  • Loading branch information
keithamus committed May 9, 2019
1 parent 3971931 commit 4cf8928
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
10 changes: 8 additions & 2 deletions lib/async.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 2 additions & 0 deletions test/mock.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 4cf8928

Please sign in to comment.