Perf: Avoid creation of intermediary array when iterating over style rules (#1272)
* Perf: Avoid creation of intermediary array when iterating over stylesheet rules by using the second `mapFn` argument of Array.from https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/from Performance analysis by: JonasBA <jonas@badalic.com> Authored-by: Eoghan Murray <eoghan@getthere.ie>
This commit is contained in:
6
.changeset/nervous-mirrors-perform.md
Normal file
6
.changeset/nervous-mirrors-perform.md
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
---
|
||||||
|
'rrweb-snapshot': patch
|
||||||
|
'rrweb': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Perf: Avoid creation of intermediary array when iterating over style rules
|
||||||
@@ -98,7 +98,7 @@ export function stringifyStylesheet(s: CSSStyleSheet): string | null {
|
|||||||
const rules = s.rules || s.cssRules;
|
const rules = s.rules || s.cssRules;
|
||||||
return rules
|
return rules
|
||||||
? fixBrowserCompatibilityIssuesInCSS(
|
? fixBrowserCompatibilityIssuesInCSS(
|
||||||
Array.from(rules).map(stringifyRule).join(''),
|
Array.from(rules, stringifyRule).join(''),
|
||||||
)
|
)
|
||||||
: null;
|
: null;
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
|||||||
@@ -61,15 +61,12 @@ export class StylesheetManager {
|
|||||||
let styleId;
|
let styleId;
|
||||||
if (!this.styleMirror.has(sheet)) {
|
if (!this.styleMirror.has(sheet)) {
|
||||||
styleId = this.styleMirror.add(sheet);
|
styleId = this.styleMirror.add(sheet);
|
||||||
const rules = Array.from(sheet.rules || CSSRule);
|
|
||||||
styles.push({
|
styles.push({
|
||||||
styleId,
|
styleId,
|
||||||
rules: rules.map((r, index) => {
|
rules: Array.from(sheet.rules || CSSRule, (r, index) => ({
|
||||||
return {
|
rule: stringifyRule(r),
|
||||||
rule: stringifyRule(r),
|
index,
|
||||||
index,
|
})),
|
||||||
};
|
|
||||||
}),
|
|
||||||
});
|
});
|
||||||
} else styleId = this.styleMirror.getId(sheet);
|
} else styleId = this.styleMirror.getId(sheet);
|
||||||
adoptedStyleSheetData.styleIds.push(styleId);
|
adoptedStyleSheetData.styleIds.push(styleId);
|
||||||
|
|||||||
Reference in New Issue
Block a user