Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance: cache browserSelection by resolved browserslist #199

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 9 additions & 12 deletions lib/BrowserSelection.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,11 @@ export default class BrowserSelection {
* `feature-name` is a caniuse-db slug.
*/
compileBrowserSupport() {
const cacheKey = JSON.stringify(this.#list);
if (BrowserSelection.#browserSupportCache.has(cacheKey)) {
return BrowserSelection.#browserSupportCache.get(cacheKey);
}

/** @type {Partial<Record<keyof FEATURES, MissingSupportResultStats>>} */
const result = {};

Expand All @@ -164,11 +169,12 @@ export default class BrowserSelection {
}
}

BrowserSelection.#browserSupportCache.set(cacheKey, result);
return result;
}

/** @type {Map<string, MissingSupportResult>} @readonly */
static #missingSupportCache = new Map();
/** @type {Map<string, BrowserSupportStats>} */
static #browserSupportCache = new Map();

/**
* @see BrowserSelection.compileBrowserSupport
Expand All @@ -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;
}
}
Loading