From 2cea1a4cdd8d676c4275cc7914ffc2c37abcc3e0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vin=C3=ADcius=20Louren=C3=A7o?= Date: Thu, 30 Mar 2023 22:37:02 -0300 Subject: [PATCH] perf: faster memoOpts for range --- classes/range.js | 44 +++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/classes/range.js b/classes/range.js index a791d912..ebef7568 100644 --- a/classes/range.js +++ b/classes/range.js @@ -31,7 +31,7 @@ class Range { this.set = range .split('||') // map the range to a 2d array of comparators - .map(r => this.parseRange(r.trim())) + .map(r => this.parseRange(r)) // throw out any comparator lists that are empty // this generally means that it was not a valid range, which is allowed // in loose mode, but will still throw if the WHOLE range is invalid. @@ -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,44 @@ class Range { return false } } + +const memoKeyAllOpts = '1'; +const memoKeyPreReleaseAndLooseOpts = '2'; +const memoKeyPreReleaseAndRtlOpts = '3'; +const memoKeyPreReleaseOpts = '4'; +const memoKeyLooseAndRtlOpts = '5'; +const memoKeyLooseOpts = '6'; +const memoKeyRtlOpts = '7'; +const memoKeyEmptyOpts = '8'; + +function buildMemoKeyFromOptions(options) { + if (options.includePrerelease === true) { + if (options.loose === true && options.rtl === true) { + return memoKeyAllOpts; + } + + if (options.loose === true) { + return memoKeyPreReleaseAndLooseOpts; + } + + if (options.rtl === true) { + return memoKeyPreReleaseAndRtlOpts; + } + + return memoKeyPreReleaseOpts; + } else if (options.loose === true) { + if (options.rtl === true) { + return memoKeyLooseAndRtlOpts; + } + + return memoKeyLooseOpts; + } else if (options.rtl === true) { + return memoKeyRtlOpts; + } else { + return memoKeyEmptyOpts; + } +} + module.exports = Range const LRU = require('lru-cache')