Skip to content

Commit bc387ec

Browse files
authored
fix(perf): address slow parse when using unknown-options-as-args (#400)
1 parent 632b3e0 commit bc387ec

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

index.js

+1
Original file line numberDiff line numberDiff line change
@@ -813,6 +813,7 @@ function parse (args, opts) {
813813
}
814814

815815
function isUnknownOption (arg) {
816+
arg = arg.replace(/^-{3,}/, '---')
816817
// ignore negative numbers
817818
if (arg.match(negative)) { return false }
818819
// if this is a short option group and all of them are configured, it isn't unknown

test/yargs-parser.js

+33
Original file line numberDiff line numberDiff line change
@@ -3301,4 +3301,37 @@ describe('yargs-parser', function () {
33013301
expect({}.foo).to.equal(undefined)
33023302
expect({}.bar).to.equal(undefined)
33033303
})
3304+
3305+
// Refs: https://github.com/yargs/yargs-parser/issues/386
3306+
describe('perf', () => {
3307+
const i = 100000
3308+
describe('unknown-options-as-args', () => {
3309+
it('parses long chain of "-" with reasonable performance', function () {
3310+
this.timeout(500)
3311+
const s = (new Array(i).fill('-').join('')) + 'a'
3312+
const parsed = parser([s], { configuration: { 'unknown-options-as-args': true } })
3313+
parsed._[0].should.equal(s)
3314+
})
3315+
it('parses long chain of "-a-a" with reasonable performance', function () {
3316+
this.timeout(500)
3317+
const s = '-' + (new Array(i).fill('-a').join('')) + '=35'
3318+
const parsed = parser([s], { configuration: { 'unknown-options-as-args': true } })
3319+
parsed._[0].should.equal(s)
3320+
})
3321+
})
3322+
it('parses long chain of "-" with reasonable performance', function () {
3323+
this.timeout(500)
3324+
const s = (new Array(i).fill('-').join('')) + 'a'
3325+
const arg = (new Array(i - 2).fill('-').join('')) + 'a'
3326+
const parsed = parser([s])
3327+
parsed[arg].should.equal(true)
3328+
})
3329+
it('parses long chain of "-a-a" with reasonable performance', function () {
3330+
this.timeout(500)
3331+
const s = '-' + (new Array(i).fill('-a').join('')) + '=35'
3332+
const arg = 'a' + (new Array(i - 1).fill('A').join(''))
3333+
const parsed = parser([s])
3334+
parsed[arg].should.equal(35)
3335+
})
3336+
})
33043337
})

0 commit comments

Comments
 (0)