Skip to content

Commit

Permalink
fix: faster cache key factory for range
Browse files Browse the repository at this point in the history
  • Loading branch information
H4ad committed Apr 6, 2023
1 parent da08e01 commit 3a87cd9
Showing 1 changed file with 31 additions and 2 deletions.
33 changes: 31 additions & 2 deletions classes/range.js
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 3a87cd9

Please sign in to comment.