Skip to content

Commit

Permalink
fix: faster cache key factory for range (#536)
Browse files Browse the repository at this point in the history
  • Loading branch information
H4ad authored Apr 10, 2023
1 parent f8b8b61 commit 61e6ea1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 6 additions & 2 deletions classes/range.js
Original file line number Diff line number Diff line change
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
2 changes: 2 additions & 0 deletions internal/constants.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,6 @@ module.exports = {
MAX_SAFE_INTEGER,
RELEASE_TYPES,
SEMVER_SPEC_VERSION,
FLAG_INCLUDE_PRERELEASE: 0b001,
FLAG_LOOSE: 0b010,
}

0 comments on commit 61e6ea1

Please sign in to comment.