From 2bc0f821b280f5df6018d5112bee074489061495 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Louren=C3=A7o?= Date: Thu, 6 Apr 2023 00:04:24 -0300 Subject: [PATCH] fix: faster cache key factory for range --- classes/range.js | 14 ++++++++++++-- internal/constants.js | 3 +++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/classes/range.js b/classes/range.js index a791d912..0db5a577 100644 --- a/classes/range.js +++ b/classes/range.js @@ -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 @@ -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') @@ -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 === '' diff --git a/internal/constants.js b/internal/constants.js index 4f0de59b..bc90bd88 100644 --- a/internal/constants.js +++ b/internal/constants.js @@ -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, }