Skip to content

Commit 609b7fa

Browse files
authored
perf(mutation): refactor parent removed detection to iterative procedure (#1489)
* perf(mutation): add deep tree benchmark * perf(mutation): use iterative procedure * perf(mutation): run formatter * perf(mutation): add changeset
1 parent d38893f commit 609b7fa

File tree

4 files changed

+50
-8
lines changed

4 files changed

+50
-8
lines changed

.changeset/kind-kids-design.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+
Optimize performance of isParentRemoved by converting it to an iterative procedure

packages/rrweb/src/record/mutation.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -802,15 +802,15 @@ function _isParentRemoved(
802802
n: Node,
803803
mirror: Mirror,
804804
): boolean {
805-
const { parentNode } = n;
806-
if (!parentNode) {
807-
return false;
808-
}
809-
const parentId = mirror.getId(parentNode);
810-
if (removes.some((r) => r.id === parentId)) {
811-
return true;
805+
let node: ParentNode | null = n.parentNode;
806+
while (node) {
807+
const parentId = mirror.getId(node);
808+
if (removes.some((r) => r.id === parentId)) {
809+
return true;
810+
}
811+
node = node.parentNode;
812812
}
813-
return _isParentRemoved(removes, parentNode, mirror);
813+
return false;
814814
}
815815

816816
function isAncestorInSet(set: Set<Node>, n: Node): boolean {

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ const suites: Array<
1818
// eval: 'document.querySelector("button").click()',
1919
// times: 10,
2020
// },
21+
{
22+
title: 'create 1000x 1 DOM nodes with deeply nested children',
23+
html: 'benchmark-dom-mutation-deep-nested.html',
24+
eval: 'window.workload()',
25+
times: 10,
26+
},
2127
{
2228
title: 'create 1000x10 DOM nodes',
2329
html: 'benchmark-dom-mutation.html',
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<html>
2+
<body></body>
3+
<script>
4+
function init() {
5+
const count = 100;
6+
7+
let roots = [];
8+
for (let i = 0; i < count; ++i) {
9+
const div = document.createElement('div');
10+
document.body.appendChild(div);
11+
roots.push(div);
12+
}
13+
14+
let tree_depth = 256;
15+
let children = [...roots];
16+
while (tree_depth > 0) {
17+
for (let i = 0; i < children.length; i++) {
18+
const div = document.createElement('div');
19+
children[i].appendChild(div);
20+
children[i] = div;
21+
}
22+
tree_depth--;
23+
}
24+
}
25+
26+
window.workload = () => {
27+
document.body.innerHTML = '';
28+
init();
29+
};
30+
</script>
31+
</html>

0 commit comments

Comments
 (0)