Skip to content

Commit 9edf714

Browse files
committed
fix: faster parse options using cached objects
1 parent da08e01 commit 9edf714

File tree

2 files changed

+60
-10
lines changed

2 files changed

+60
-10
lines changed

internal/parse-options.js

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,46 @@
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) return emptyOpts
14+
15+
if (typeof options !== 'object') return var6
16+
17+
if (options[isParsedConfigSymbol])
18+
return options;
19+
20+
if (options.includePrerelease) {
21+
if (options.loose && options.rtl) {
22+
return var1
23+
}
24+
25+
if (options.loose) {
26+
return var2
27+
}
28+
29+
if (options.rtl) {
30+
return var3
31+
}
32+
33+
return var4
34+
} else if (options.loose) {
35+
if (options.rtl) {
36+
return var5
37+
}
38+
39+
return var6
40+
} else if (options.rtl) {
41+
return var7
42+
} else {
43+
return emptyOpts
44+
}
45+
}
1146
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)