perf(rrweb): attribute mutation optimization (#1343)
This commit is contained in:
committed by
GitHub
parent
6dafa924d2
commit
a8dcca54d8
5
.changeset/moody-dots-refuse.md
Normal file
5
.changeset/moody-dots-refuse.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'rrweb': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
use WeakMap for faster attributeCursor lookup while processing attribute mutations
|
||||||
@@ -142,6 +142,7 @@ export default class MutationBuffer {
|
|||||||
|
|
||||||
private texts: textCursor[] = [];
|
private texts: textCursor[] = [];
|
||||||
private attributes: attributeCursor[] = [];
|
private attributes: attributeCursor[] = [];
|
||||||
|
private attributeMap = new WeakMap<Node, attributeCursor>();
|
||||||
private removes: removedNodeMutation[] = [];
|
private removes: removedNodeMutation[] = [];
|
||||||
private mapRemoves: Node[] = [];
|
private mapRemoves: Node[] = [];
|
||||||
|
|
||||||
@@ -485,6 +486,7 @@ export default class MutationBuffer {
|
|||||||
// reset
|
// reset
|
||||||
this.texts = [];
|
this.texts = [];
|
||||||
this.attributes = [];
|
this.attributes = [];
|
||||||
|
this.attributeMap = new WeakMap<Node, attributeCursor>();
|
||||||
this.removes = [];
|
this.removes = [];
|
||||||
this.addedSet = new Set<Node>();
|
this.addedSet = new Set<Node>();
|
||||||
this.movedSet = new Set<Node>();
|
this.movedSet = new Set<Node>();
|
||||||
@@ -554,9 +556,7 @@ export default class MutationBuffer {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
let item: attributeCursor | undefined = this.attributes.find(
|
let item = this.attributeMap.get(m.target);
|
||||||
(a) => a.node === m.target,
|
|
||||||
);
|
|
||||||
if (
|
if (
|
||||||
target.tagName === 'IFRAME' &&
|
target.tagName === 'IFRAME' &&
|
||||||
attributeName === 'src' &&
|
attributeName === 'src' &&
|
||||||
@@ -578,6 +578,7 @@ export default class MutationBuffer {
|
|||||||
_unchangedStyles: {},
|
_unchangedStyles: {},
|
||||||
};
|
};
|
||||||
this.attributes.push(item);
|
this.attributes.push(item);
|
||||||
|
this.attributeMap.set(m.target, item);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Keep this property on inputs that used to be password inputs
|
// Keep this property on inputs that used to be password inputs
|
||||||
|
|||||||
@@ -42,6 +42,12 @@ const suites: Array<
|
|||||||
eval: 'window.workload()',
|
eval: 'window.workload()',
|
||||||
times: 5,
|
times: 5,
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
title: 'modify attributes on 10000 DOM nodes',
|
||||||
|
html: 'benchmark-dom-mutation-attributes.html',
|
||||||
|
eval: 'window.workload()',
|
||||||
|
times: 10,
|
||||||
|
},
|
||||||
];
|
];
|
||||||
|
|
||||||
function avg(v: number[]): number {
|
function avg(v: number[]): number {
|
||||||
|
|||||||
@@ -0,0 +1,21 @@
|
|||||||
|
<html>
|
||||||
|
<body></body>
|
||||||
|
<script>
|
||||||
|
function init() {
|
||||||
|
const count = 10000;
|
||||||
|
for (let i = 0; i < count; ++i) {
|
||||||
|
const div = document.createElement('div');
|
||||||
|
document.body.appendChild(div);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
init();
|
||||||
|
|
||||||
|
window.workload = () => {
|
||||||
|
const divs = document.getElementsByTagName('div');
|
||||||
|
for (let div of divs) {
|
||||||
|
div.classList.add('foo');
|
||||||
|
}
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
</html>
|
||||||
Reference in New Issue
Block a user