Skip to content

Commit 5cf24b5

Browse files
committed
fix: faster parse options using cached objects
1 parent da08e01 commit 5cf24b5

File tree

2 files changed

+65
-10
lines changed

2 files changed

+65
-10
lines changed

internal/parse-options.js

Lines changed: 50 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,51 @@
1-
// parse out just the options we care about so we always get a consistent
2-
// obj with keys in a consistent order.
3-
const opts = ['includePrerelease', 'loose', 'rtl']
4-
const parseOptions = options =>
5-
!options ? {}
6-
: typeof options !== 'object' ? { loose: true }
7-
: opts.filter(k => options[k]).reduce((o, k) => {
8-
o[k] = true
9-
return o
10-
}, {})
1+
// parse out just the options we care about
2+
const isParsedConfigSymbol = Symbol('isParsedConfig')
3+
const var1 = Object.freeze({ includePrerelease: true, loose: true, rtl: true, [isParsedConfigSymbol]: true })
4+
const var2 = Object.freeze({ includePrerelease: true, loose: true, [isParsedConfigSymbol]: true })
5+
const var3 = Object.freeze({ includePrerelease: true, rtl: true, [isParsedConfigSymbol]: true })
6+
const var4 = Object.freeze({ includePrerelease: true, [isParsedConfigSymbol]: true })
7+
const var5 = Object.freeze({ loose: true, rtl: true, [isParsedConfigSymbol]: true })
8+
const var6 = Object.freeze({ loose: true, [isParsedConfigSymbol]: true })
9+
const var7 = Object.freeze({ rtl: true, [isParsedConfigSymbol]: true })
10+
const emptyOpts = Object.freeze({ [isParsedConfigSymbol]: true })
11+
12+
const parseOptions = options => {
13+
if (!options) {
14+
return emptyOpts
15+
}
16+
17+
if (typeof options !== 'object') {
18+
return var6
19+
}
20+
21+
if (options[isParsedConfigSymbol]) {
22+
return options
23+
}
24+
25+
if (options.includePrerelease) {
26+
if (options.loose && options.rtl) {
27+
return var1
28+
}
29+
30+
if (options.loose) {
31+
return var2
32+
}
33+
34+
if (options.rtl) {
35+
return var3
36+
}
37+
38+
return var4
39+
} else if (options.loose) {
40+
if (options.rtl) {
41+
return var5
42+
}
43+
44+
return var6
45+
} else if (options.rtl) {
46+
return var7
47+
} else {
48+
return emptyOpts
49+
}
50+
}
1151
module.exports = parseOptions

test/internal/parse-options.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,20 @@ t.test('objects only include truthy flags we know about, set to true', t => {
2727
rtl: true,
2828
includePrerelease: true,
2929
})
30+
t.strictSame(parseOptions({ loose: true }), { loose: true })
31+
t.strictSame(parseOptions({ rtl: true }), { rtl: true })
32+
t.strictSame(parseOptions({ includePrerelease: true }), { includePrerelease: true })
33+
t.strictSame(parseOptions({ loose: true, rtl: true }), { loose: true, rtl: true })
34+
t.strictSame(parseOptions({ loose: true, includePrerelease: true }), { loose: true, includePrerelease: true })
35+
t.strictSame(parseOptions({ rtl: true, includePrerelease: true }), { rtl: true, includePrerelease: true })
36+
t.end()
37+
})
38+
39+
t.test('should skip validation when options is already parsed', t => {
40+
const options = { loose: true, rtl: true }
41+
const parsedOptions = parseOptions(options)
42+
43+
t.equal(parseOptions(parsedOptions) === parsedOptions, true)
44+
t.not(parseOptions(options) === parsedOptions, false)
3045
t.end()
3146
})

0 commit comments

Comments
 (0)