Skip to content

Commit

Permalink
Perf: Avoid creation of intermediary array when iterating over style …
Browse files Browse the repository at this point in the history
…rules (rrweb-io#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 <[email protected]>
Authored-by: Eoghan Murray <[email protected]>
  • Loading branch information
eoghanmurray committed Aug 15, 2023
1 parent 64420c7 commit 58c9104
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 8 deletions.
6 changes: 6 additions & 0 deletions .changeset/nervous-mirrors-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'rrweb-snapshot': patch
'rrweb': patch
---

Perf: Avoid creation of intermediary array when iterating over style rules
2 changes: 1 addition & 1 deletion packages/rrweb-snapshot/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 4 additions & 7 deletions packages/rrweb/src/record/stylesheet-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
rule: stringifyRule(r),
index,
};
}),
rules: Array.from(sheet.rules || CSSRule, (r, index) => ({
rule: stringifyRule(r),
index,
})),
});
} else styleId = this.styleMirror.getId(sheet);
adoptedStyleSheetData.styleIds.push(styleId);
Expand Down

0 comments on commit 58c9104

Please sign in to comment.