From 3a87cd904a0617efa64223c5226cce8fbb3651fc 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 | 33 +++++++++++++++++++++++++++++++-- 1 file changed, 31 insertions(+), 2 deletions(-) diff --git a/classes/range.js b/classes/range.js index a791d912..29598bff 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,35 @@ class Range { return false } } + +function buildMemoKeyFromOptions (options) { + if (options.includePrerelease === true) { + if (options.loose === true && options.rtl === true) { + return '1' + } + + if (options.loose === true) { + return '2' + } + + if (options.rtl === true) { + return '3' + } + + return '4' + } else if (options.loose === true) { + if (options.rtl === true) { + return '5' + } + + return '6' + } else if (options.rtl === true) { + return '7' + } else { + return '8' + } +} + module.exports = Range const LRU = require('lru-cache')