Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: performance improvement #53

Merged
merged 4 commits into from
Sep 29, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ var isWin32 = require('os').platform() === 'win32';

var slash = '/';
var backslash = /\\/g;
var globby = /(^|[^\\])([{[]|\([^)]+$)/;
var escaped = /\\([!*?|[\](){}])/g;

/**
Expand All @@ -33,7 +32,7 @@ module.exports = function globParent(str, opts) {
// remove path parts that are globby
do {
str = pathPosixDirname(str);
} while (isGlob(str) || globby.test(str));
} while (isGlobby(str));

// remove escape chars and return result
return str.replace(escaped, '$1');
Expand Down Expand Up @@ -61,3 +60,16 @@ function isEnclosure(str) {

return str.slice(foundIndex + 1, -1).includes(slash);
}

function isGlobby(str) {
if (/\([^()]+$/.test(str)) {
return true;
}
if (str[0] === '{' || str[0] === '[') {
return true;
}
if (/[^\\][{[]/.test(str)) {
return true;
}
return isGlob(str);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there anything weird (different?) by changing the order of the isGlob check and "globby" regexp test?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because they were OR'ed, I don't think it makes any difference logically. I'm happy to flip it back assuming that doesn't introduce performance regressions.

}
6 changes: 6 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,12 @@ describe('glob2base test patterns', function () {
gp('('.repeat(500000));
done();
});

it("should finish in reasonable time for '/('.repeat(n) + ')'", function (done) {
this.timeout(1000);
gp('/('.repeat(500000) + ')');
done();
});
});

if (isWin32) {
Expand Down