Skip to content

Commit 814c18f

Browse files
committed
Performance refactor to avoid 2 function calls (bridging between rrweb-io#1543 and rrweb-io#1652)
1 parent dfb2991 commit 814c18f

File tree

2 files changed

+16
-25
lines changed

2 files changed

+16
-25
lines changed

.changeset/removes-and-descendants.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+
Performance refactor to avoid 2 function calls (bridging between #1543 and #1652)

packages/rrweb/src/record/mutation.ts

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import {
66
isShadowRoot,
77
needMaskingText,
88
maskInputValue,
9-
Mirror,
109
isNativeShadowDom,
1110
getInputType,
1211
toLowerCase,
@@ -169,7 +168,7 @@ export default class MutationBuffer {
169168
private addedSet = new Set<Node>();
170169
private movedSet = new Set<Node>();
171170
private droppedSet = new Set<Node>();
172-
private removesSubTreeCache = new Set<Node>();
171+
private removesAndDescendants = new Set<Node>();
173172

174173
private mutationCb: observerParam['mutationCb'];
175174
private blockClass: observerParam['blockClass'];
@@ -367,9 +366,11 @@ export default class MutationBuffer {
367366
}
368367

369368
for (const n of this.movedSet) {
369+
const movedParent = dom.parentNode(n);
370370
if (
371-
isParentRemoved(this.removesSubTreeCache, n, this.mirror) &&
372-
!this.movedSet.has(dom.parentNode(n)!)
371+
movedParent && // can't be removed if it doesn't exist
372+
this.removesAndDescendants.has(movedParent) &&
373+
!this.movedSet.has(movedParent)
373374
) {
374375
continue;
375376
}
@@ -379,7 +380,7 @@ export default class MutationBuffer {
379380
for (const n of this.addedSet) {
380381
if (
381382
!isAncestorInSet(this.droppedSet, n) &&
382-
!isParentRemoved(this.removesSubTreeCache, n, this.mirror)
383+
!this.removesAndDescendants.has(dom.parentNode(n)!)
383384
) {
384385
pushAdd(n);
385386
} else if (isAncestorInSet(this.movedSet, n)) {
@@ -515,7 +516,7 @@ export default class MutationBuffer {
515516
this.addedSet = new Set<Node>();
516517
this.movedSet = new Set<Node>();
517518
this.droppedSet = new Set<Node>();
518-
this.removesSubTreeCache = new Set<Node>();
519+
this.removesAndDescendants = new Set<Node>();
519520
this.movedMap = {};
520521

521522
this.mutationCb(payload);
@@ -750,7 +751,7 @@ export default class MutationBuffer {
750751
? true
751752
: undefined,
752753
});
753-
processRemoves(n, this.removesSubTreeCache);
754+
populateWithDescendants(n, this.removesAndDescendants);
754755
}
755756
this.mapRemoves.push(n);
756757
});
@@ -814,35 +815,20 @@ function deepDelete(addsSet: Set<Node>, n: Node) {
814815
dom.childNodes(n).forEach((childN) => deepDelete(addsSet, childN));
815816
}
816817

817-
function processRemoves(n: Node, cache: Set<Node>) {
818+
function populateWithDescendants(n: Node, s: Set<Node>) {
818819
const queue = [n];
819820

820821
while (queue.length) {
821822
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
822823
const next = queue.pop()!;
823-
if (cache.has(next)) continue;
824-
cache.add(next);
824+
if (s.has(next)) continue;
825+
s.add(next);
825826
dom.childNodes(next).forEach((n) => queue.push(n));
826827
}
827828

828829
return;
829830
}
830831

831-
function isParentRemoved(removes: Set<Node>, n: Node, mirror: Mirror): boolean {
832-
if (removes.size === 0) return false;
833-
return _isParentRemoved(removes, n, mirror);
834-
}
835-
836-
function _isParentRemoved(
837-
removes: Set<Node>,
838-
n: Node,
839-
_mirror: Mirror,
840-
): boolean {
841-
const node: ParentNode | null = dom.parentNode(n);
842-
if (!node) return false;
843-
return removes.has(node);
844-
}
845-
846832
function isAncestorInSet(set: Set<Node>, n: Node): boolean {
847833
if (set.size === 0) return false;
848834
return _isAncestorInSet(set, n);

0 commit comments

Comments
 (0)