From 1db95907e6dbfed553666add26dce9d72a47ff97 Mon Sep 17 00:00:00 2001 From: huliang <498755303@qq.com> Date: Wed, 22 Jul 2026 22:42:05 +0800 Subject: [PATCH] fix: cache isFireFox() result in CSSParser to avoid repeated UA scan recordResult() runs once per CSS fragment and called isFireFox() every time, so a single stylesheet triggers thousands of avoidable UA substring scans. Cache the result into this.isFF once at the start of exec(), reuse it in recordResult() and the return value, and reset it in reset(). --- src/sandbox/scoped_css.ts | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/sandbox/scoped_css.ts b/src/sandbox/scoped_css.ts index 1fadfa33..f8bed36e 100644 --- a/src/sandbox/scoped_css.ts +++ b/src/sandbox/scoped_css.ts @@ -35,6 +35,7 @@ class CSSParser { private scopecssDisableSelectors: Array = [] // disable or enable scopecss for specific selectors private scopecssDisableNextLine = false // use block comments /* scopecss-disable-next-line */ to disable scopecss on a specific line private optionCssSelectors: Array = [] // use this option to include specific selectors, so that they will be affected by scopecss ,like microApp.options.optionCss + private isFF = false // cache isFireFox() result per exec, avoid repeated UA scan in recordResult public exec ( cssText: string, @@ -46,11 +47,13 @@ class CSSParser { this.prefix = prefix this.baseURI = baseURI this.linkPath = linkPath || '' + // cache isFireFox() once per exec, recordResult runs for every fragment + this.isFF = isFireFox() // fetch optionCss configure this.optionCssSelectors = microApp.options.optionCss || [] this.matchRules() - return isFireFox() ? decodeURIComponent(this.result) : this.result + return this.isFF ? decodeURIComponent(this.result) : this.result } public reset (): void { @@ -58,6 +61,7 @@ class CSSParser { this.scopecssDisable = this.scopecssDisableNextLine = false this.scopecssDisableSelectors = [] this.optionCssSelectors = [] + this.isFF = false } // core action for match rules @@ -471,7 +475,7 @@ class CSSParser { // splice string private recordResult (strFragment: string): void { // Firefox performance degradation when string contain special characters, see https://github.com/jd-opensource/micro-app/issues/256 - if (isFireFox()) { + if (this.isFF) { this.result += encodeURIComponent(strFragment) } else { this.result += strFragment