Skip to content

Commit 9b1364a

Browse files
committed
fix: faster cache key factory for range
1 parent da08e01 commit 9b1364a

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

classes/range.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ class Range {
8181

8282
// memoize range parsing for performance.
8383
// this is a very hot path, and fully deterministic.
84-
const memoOpts = Object.keys(this.options).join(',')
85-
const memoKey = `parseRange:${memoOpts}:${range}`
84+
const memoOpts = buildMemoKeyFromOptions(this.options)
85+
const memoKey = memoOpts + ':' + range
8686
const cached = cache.get(memoKey)
8787
if (cached) {
8888
return cached
@@ -190,6 +190,14 @@ class Range {
190190
return false
191191
}
192192
}
193+
194+
function buildMemoKeyFromOptions (options) {
195+
return (
196+
(options.includePrerelease ? FLAG_INCLUDE_PRERELEASE : 0)
197+
| (options.loose ? FLAG_LOOSE : 0)
198+
) + ''
199+
}
200+
193201
module.exports = Range
194202

195203
const LRU = require('lru-cache')
@@ -206,6 +214,7 @@ const {
206214
tildeTrimReplace,
207215
caretTrimReplace,
208216
} = require('../internal/re')
217+
const { FLAG_INCLUDE_PRERELEASE, FLAG_LOOSE } = require('../internal/constants')
209218

210219
const isNullSet = c => c.value === '<0.0.0-0'
211220
const isAny = c => c.value === ''

internal/constants.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,7 @@ module.exports = {
1414
MAX_LENGTH,
1515
MAX_SAFE_INTEGER,
1616
MAX_SAFE_COMPONENT_LENGTH,
17+
FLAG_INCLUDE_PRERELEASE: 0b001,
18+
FLAG_LOOSE: 0b010,
19+
FLAG_RTL: 0b100,
1720
}

0 commit comments

Comments
 (0)