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 2bc0f82
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
14 changes: 12 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,15 @@ class Range {
return false
}
}

function buildMemoKeyFromOptions (options) {
return (
(options.includePrerelease ? FLAG_INCLUDE_PRERELEASE : 0)
| (options.loose ? FLAG_LOOSE : 0)
| (options.rtl ? FLAG_RTL : 0)
) + ''
}

module.exports = Range

const LRU = require('lru-cache')
Expand All @@ -206,6 +215,7 @@ const {
tildeTrimReplace,
caretTrimReplace,
} = require('../internal/re')
const { FLAG_INCLUDE_PRERELEASE, FLAG_LOOSE, FLAG_RTL } = 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: 1 << 1,
FLAG_LOOSE: 1 << 2,
FLAG_RTL: 1 << 3,
}

0 comments on commit 2bc0f82

Please sign in to comment.