Skip to content

Commit

Permalink
perf: faster memoOpts for range
Browse files Browse the repository at this point in the history
  • Loading branch information
H4ad committed Mar 31, 2023
1 parent ca185a0 commit 8a20c28
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions classes/range.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class Range {
this.set = range
.split('||')
// map the range to a 2d array of comparators
.map(r => this.parseRange(r.trim()))
.map(r => this.parseRange(r))
// throw out any comparator lists that are empty
// this generally means that it was not a valid range, which is allowed
// in loose mode, but will still throw if the WHOLE range is invalid.
Expand Down Expand Up @@ -81,8 +81,8 @@ class Range {

// memoize range parsing for performance.
// this is a very hot path, and fully deterministic.
const memoOpts = Object.keys(this.options).join(',')
const memoKey = `parseRange:${memoOpts}:${range}`
const memoOpts = buildMemoKeyFromOptions(this.options)
const memoKey = memoOpts + range
const cached = cache.get(memoKey)
if (cached) {
return cached
Expand Down Expand Up @@ -190,6 +190,35 @@ class Range {
return false
}
}

function buildMemoKeyFromOptions(options) {
if (options.includePrerelease === true) {
if (options.loose === true && options.rtl === true) {
return '1';
}

if (options.loose === true) {
return '2';
}

if (options.rtl === true) {
return '3';
}

return '4';
} else if (options.loose === true) {
if (options.rtl === true) {
return '5';
}

return '6';
} else if (options.rtl === true) {
return '7';
} else {
return '8';
}
}

module.exports = Range

const LRU = require('lru-cache')
Expand Down

0 comments on commit 8a20c28

Please sign in to comment.