From c20342a38b5432eaf76982743471dac9d8e5a983 Mon Sep 17 00:00:00 2001 From: vklepov Date: Thu, 9 Jan 2025 21:40:39 +0300 Subject: [PATCH 1/2] cache browserSelection by resolved browserslist --- lib/BrowserSelection.js | 23 ++++++++++------------- 1 file changed, 10 insertions(+), 13 deletions(-) diff --git a/lib/BrowserSelection.js b/lib/BrowserSelection.js index 5ee2a84..abcb72d 100644 --- a/lib/BrowserSelection.js +++ b/lib/BrowserSelection.js @@ -29,6 +29,9 @@ import { formatBrowserName } from '../utils/util.js'; * @prop {Partial>} features */ +/** @type {Map} */ +const browserSupportCache = new Map(); + export default class BrowserSelection { #list; @@ -139,6 +142,11 @@ export default class BrowserSelection { * `feature-name` is a caniuse-db slug. */ compileBrowserSupport() { + const cacheKey = JSON.stringify(this.#list); + if (browserSupportCache.has(cacheKey)) { + return browserSupportCache.get(cacheKey); + } + /** @type {Partial>} */ const result = {}; @@ -164,12 +172,10 @@ export default class BrowserSelection { } } + browserSupportCache.set(cacheKey, result); return result; } - /** @type {Map} @readonly */ - static #missingSupportCache = new Map(); - /** * @see BrowserSelection.compileBrowserSupport * @param {ConstructorParameters} constructorParameters @@ -177,19 +183,10 @@ export default class BrowserSelection { */ static missingSupport(...constructorParameters) { const [query, path] = constructorParameters; - // browserslist only uses `path` if `query` is not provided. - const key = query ? JSON.stringify(query) : path; - - if (key && this.#missingSupportCache.has(key)) { - return this.#missingSupportCache.get(key); - } - const selection = new BrowserSelection(query, path); - const result = { + return { browsers: selection.list(), features: selection.compileBrowserSupport(), }; - if (key) this.#missingSupportCache.set(key, result); - return result; } } From ffaf31ecb455cab98d80fb3d334d699067e3228b Mon Sep 17 00:00:00 2001 From: vklepov Date: Mon, 13 Jan 2025 18:49:45 +0300 Subject: [PATCH 2/2] move cache to static field --- lib/BrowserSelection.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/BrowserSelection.js b/lib/BrowserSelection.js index abcb72d..16d8953 100644 --- a/lib/BrowserSelection.js +++ b/lib/BrowserSelection.js @@ -29,9 +29,6 @@ import { formatBrowserName } from '../utils/util.js'; * @prop {Partial>} features */ -/** @type {Map} */ -const browserSupportCache = new Map(); - export default class BrowserSelection { #list; @@ -143,8 +140,8 @@ export default class BrowserSelection { */ compileBrowserSupport() { const cacheKey = JSON.stringify(this.#list); - if (browserSupportCache.has(cacheKey)) { - return browserSupportCache.get(cacheKey); + if (BrowserSelection.#browserSupportCache.has(cacheKey)) { + return BrowserSelection.#browserSupportCache.get(cacheKey); } /** @type {Partial>} */ @@ -172,10 +169,13 @@ export default class BrowserSelection { } } - browserSupportCache.set(cacheKey, result); + BrowserSelection.#browserSupportCache.set(cacheKey, result); return result; } + /** @type {Map} */ + static #browserSupportCache = new Map(); + /** * @see BrowserSelection.compileBrowserSupport * @param {ConstructorParameters} constructorParameters