From 4a642f77a526b1b8cce187363ef7ee8fa353e06e Mon Sep 17 00:00:00 2001 From: tophf Date: Sun, 26 Mar 2017 10:42:13 +0300 Subject: [PATCH] Cache last 100 getDomains for filterStyles --- storage.js | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/storage.js b/storage.js index d695d0a2..a56bfdce 100644 --- a/storage.js +++ b/storage.js @@ -37,6 +37,7 @@ var cachedStyles, prefs; byId: new Map(), filters: new Map(), regexps: new Map(), + urlDomains: new Map(), mutex: { inProgress: false, onDone: [], @@ -196,6 +197,14 @@ function filterStyles(options = {}) { : cached.styles; } + if (matchUrl && !cachedStyles.urlDomains.has(matchUrl)) { + cachedStyles.urlDomains.set(matchUrl, getDomains(matchUrl)); + for (let i = cachedStyles.urlDomains.size - 100; i > 0; i--) { + const firstKey = cachedStyles.urlDomains.keys().next().value; + cachedStyles.urlDomains.delete(firstKey); + } + } + const styles = id === null ? (code ? cachedStyles.list : cachedStyles.noCode) : [(cachedStyles.byId.get(id) || {})[code ? 'style' : 'noCode']]; @@ -457,11 +466,10 @@ function sectionAppliesToUrl(section, url) { return true; } } - if (section.domains.length) { - for (const domain of getDomains(url)) { - if (section.domains.indexOf(domain) != -1) { - return true; - } + const urlDomains = cachedStyles.urlDomains.get(url) || getDomains(url); + for (const domain of urlDomains) { + if (section.domains.indexOf(domain) != -1) { + return true; } } for (const regexp of section.regexps) { @@ -490,6 +498,7 @@ function isCheckbox(el) { return el.localName == 'input' && el.type == 'checkbox'; } + // js engine can't optimize the entire function if it contains try-catch // so we should keep it isolated from normal code in a minimal wrapper // Update: might get fixed in V8 TurboFan in the future