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(perf): address slow parse when using unknown-options-as-args #400

Merged
merged 1 commit into from
Jun 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,7 @@ function parse (args, opts) {
}

function isUnknownOption (arg) {
arg = arg.replace(/^-{3,}/, '---')
// ignore negative numbers
if (arg.match(negative)) { return false }
// if this is a short option group and all of them are configured, it isn't unknown
Expand Down
33 changes: 33 additions & 0 deletions test/yargs-parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -3301,4 +3301,37 @@ describe('yargs-parser', function () {
expect({}.foo).to.equal(undefined)
expect({}.bar).to.equal(undefined)
})

// Refs: https://github.com/yargs/yargs-parser/issues/386
describe('perf', () => {
const i = 100000
describe('unknown-options-as-args', () => {
it('parses long chain of "-" with reasonable performance', function () {
this.timeout(500)
const s = (new Array(i).fill('-').join('')) + 'a'
const parsed = parser([s], { configuration: { 'unknown-options-as-args': true } })
parsed._[0].should.equal(s)
})
it('parses long chain of "-a-a" with reasonable performance', function () {
this.timeout(500)
const s = '-' + (new Array(i).fill('-a').join('')) + '=35'
const parsed = parser([s], { configuration: { 'unknown-options-as-args': true } })
parsed._[0].should.equal(s)
})
})
it('parses long chain of "-" with reasonable performance', function () {
this.timeout(500)
const s = (new Array(i).fill('-').join('')) + 'a'
const arg = (new Array(i - 2).fill('-').join('')) + 'a'
const parsed = parser([s])
parsed[arg].should.equal(true)
})
it('parses long chain of "-a-a" with reasonable performance', function () {
this.timeout(500)
const s = '-' + (new Array(i).fill('-a').join('')) + '=35'
const arg = 'a' + (new Array(i - 1).fill('A').join(''))
const parsed = parser([s])
parsed[arg].should.equal(35)
})
})
})