Skip to content

Commit 2cea1a4

Browse files
committed
perf: faster memoOpts for range
1 parent ca185a0 commit 2cea1a4

File tree

1 file changed

+41
-3
lines changed

1 file changed

+41
-3
lines changed

classes/range.js

+41-3
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class Range {
3131
this.set = range
3232
.split('||')
3333
// map the range to a 2d array of comparators
34-
.map(r => this.parseRange(r.trim()))
34+
.map(r => this.parseRange(r))
3535
// throw out any comparator lists that are empty
3636
// this generally means that it was not a valid range, which is allowed
3737
// in loose mode, but will still throw if the WHOLE range is invalid.
@@ -81,8 +81,8 @@ class Range {
8181

8282
// memoize range parsing for performance.
8383
// this is a very hot path, and fully deterministic.
84-
const memoOpts = Object.keys(this.options).join(',')
85-
const memoKey = `parseRange:${memoOpts}:${range}`
84+
const memoOpts = buildMemoKeyFromOptions(this.options)
85+
const memoKey = memoOpts + range
8686
const cached = cache.get(memoKey)
8787
if (cached) {
8888
return cached
@@ -190,6 +190,44 @@ class Range {
190190
return false
191191
}
192192
}
193+
194+
const memoKeyAllOpts = '1';
195+
const memoKeyPreReleaseAndLooseOpts = '2';
196+
const memoKeyPreReleaseAndRtlOpts = '3';
197+
const memoKeyPreReleaseOpts = '4';
198+
const memoKeyLooseAndRtlOpts = '5';
199+
const memoKeyLooseOpts = '6';
200+
const memoKeyRtlOpts = '7';
201+
const memoKeyEmptyOpts = '8';
202+
203+
function buildMemoKeyFromOptions(options) {
204+
if (options.includePrerelease === true) {
205+
if (options.loose === true && options.rtl === true) {
206+
return memoKeyAllOpts;
207+
}
208+
209+
if (options.loose === true) {
210+
return memoKeyPreReleaseAndLooseOpts;
211+
}
212+
213+
if (options.rtl === true) {
214+
return memoKeyPreReleaseAndRtlOpts;
215+
}
216+
217+
return memoKeyPreReleaseOpts;
218+
} else if (options.loose === true) {
219+
if (options.rtl === true) {
220+
return memoKeyLooseAndRtlOpts;
221+
}
222+
223+
return memoKeyLooseOpts;
224+
} else if (options.rtl === true) {
225+
return memoKeyRtlOpts;
226+
} else {
227+
return memoKeyEmptyOpts;
228+
}
229+
}
230+
193231
module.exports = Range
194232

195233
const LRU = require('lru-cache')

0 commit comments

Comments
 (0)