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:
Eoghan Murray
2026-04-01 12:00:00 +08:00
committed by GitHub
parent 816d65ec3f
commit 705a03a84e
3 changed files with 11 additions and 8 deletions

View File

@@ -0,0 +1,6 @@
---
'rrweb-snapshot': patch
'rrweb': patch
---
Perf: Avoid creation of intermediary array when iterating over style rules

View File

@@ -98,7 +98,7 @@ export function stringifyStylesheet(s: CSSStyleSheet): string | null {
const rules = s.rules || s.cssRules;
return rules
? fixBrowserCompatibilityIssuesInCSS(
Array.from(rules).map(stringifyRule).join(''),
Array.from(rules, stringifyRule).join(''),
)
: null;
} catch (error) {

View File

@@ -61,15 +61,12 @@ export class StylesheetManager {
let styleId;
if (!this.styleMirror.has(sheet)) {
styleId = this.styleMirror.add(sheet);
const rules = Array.from(sheet.rules || CSSRule);
styles.push({
styleId,
rules: rules.map((r, index) => {
return {
rules: Array.from(sheet.rules || CSSRule, (r, index) => ({
rule: stringifyRule(r),
index,
};
}),
})),
});
} else styleId = this.styleMirror.getId(sheet);
adoptedStyleSheetData.styleIds.push(styleId);