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 6a0f846
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
8 changes: 6 additions & 2 deletions classes/range.js
Expand Up @@ -81,8 +81,10 @@ 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 =
(this.options.includePrerelease && FLAG_INCLUDE_PRERELEASE) |
(this.options.loose && FLAG_LOOSE)
const memoKey = memoOpts + ':' + range
const cached = cache.get(memoKey)
if (cached) {
return cached
Expand Down Expand Up @@ -190,6 +192,7 @@ class Range {
return false
}
}

module.exports = Range

const LRU = require('lru-cache')
Expand All @@ -206,6 +209,7 @@ const {
tildeTrimReplace,
caretTrimReplace,
} = require('../internal/re')
const { FLAG_INCLUDE_PRERELEASE, FLAG_LOOSE } = require('../internal/constants')

const isNullSet = c => c.value === '<0.0.0-0'
const isAny = c => c.value === ''
Expand Down
3 changes: 3 additions & 0 deletions internal/constants.js
Expand Up @@ -14,4 +14,7 @@ module.exports = {
MAX_LENGTH,
MAX_SAFE_INTEGER,
MAX_SAFE_COMPONENT_LENGTH,
FLAG_INCLUDE_PRERELEASE: 0b001,
FLAG_LOOSE: 0b010,
FLAG_RTL: 0b100,
}

0 comments on commit 6a0f846

Please sign in to comment.