Skip to content

Commit 02e711d

Browse files
billyvgmdellanoce
andauthored
upstream: perf(rrweb): attribute mutation optimization (#178)
Ref: rrweb-io#1343 Co-authored-by: Michael Dellanoce <[email protected]>
1 parent 15c4677 commit 02e711d

File tree

4 files changed

+36
-3
lines changed

4 files changed

+36
-3
lines changed

.changeset/moody-dots-refuse.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'rrweb': patch
3+
---
4+
5+
use WeakMap for faster attributeCursor lookup while processing attribute mutations

packages/rrweb/src/record/mutation.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ export default class MutationBuffer {
144144

145145
private texts: textCursor[] = [];
146146
private attributes: attributeCursor[] = [];
147+
private attributeMap = new WeakMap<Node, attributeCursor>();
147148
private removes: removedNodeMutation[] = [];
148149
private mapRemoves: Node[] = [];
149150

@@ -502,6 +503,7 @@ export default class MutationBuffer {
502503
// reset
503504
this.texts = [];
504505
this.attributes = [];
506+
this.attributeMap = new WeakMap<Node, attributeCursor>();
505507
this.removes = [];
506508
this.addedSet = new Set<Node>();
507509
this.movedSet = new Set<Node>();
@@ -602,9 +604,7 @@ export default class MutationBuffer {
602604
return;
603605
}
604606

605-
let item: attributeCursor | undefined = this.attributes.find(
606-
(a) => a.node === m.target,
607-
);
607+
let item = this.attributeMap.get(m.target);
608608
if (
609609
target.tagName === 'IFRAME' &&
610610
attributeName === 'src' &&
@@ -626,6 +626,7 @@ export default class MutationBuffer {
626626
_unchangedStyles: {},
627627
};
628628
this.attributes.push(item);
629+
this.attributeMap.set(m.target, item);
629630
}
630631

631632
// Keep this property on inputs that used to be password inputs

packages/rrweb/test/benchmark/dom-mutation.test.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ const suites: Array<
4646
eval: 'window.workload()',
4747
times: 5,
4848
},
49+
{
50+
title: 'modify attributes on 10000 DOM nodes',
51+
html: 'benchmark-dom-mutation-attributes.html',
52+
eval: 'window.workload()',
53+
times: 10,
54+
},
4955
{
5056
title: 'mask 1000x10 DOM nodes',
5157
html: 'benchmark-text-masking.html',
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<html>
2+
<body></body>
3+
<script>
4+
function init() {
5+
const count = 10000;
6+
for (let i = 0; i < count; ++i) {
7+
const div = document.createElement('div');
8+
document.body.appendChild(div);
9+
}
10+
}
11+
12+
init();
13+
14+
window.workload = () => {
15+
const divs = document.getElementsByTagName('div');
16+
for (let div of divs) {
17+
div.classList.add('foo');
18+
}
19+
};
20+
</script>
21+
</html>

0 commit comments

Comments
 (0)