Skip to content

Commit

Permalink
optimize isParentRemoved for large remove lists
Browse files Browse the repository at this point in the history
  • Loading branch information
mdellanoce authored and jaj1014 committed Apr 22, 2024
1 parent e4ad6c0 commit a9fc91a
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/late-keys-whisper.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'rrweb': patch
---

Optimize isParentRemoved for large remove lists
19 changes: 11 additions & 8 deletions packages/rrweb/src/record/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export default class MutationBuffer {
private attributes: attributeCursor[] = [];
private attributeMap = new WeakMap<Node, attributeCursor>();
private removes: removedNodeMutation[] = [];
private removesMap = new Map<number, number>();
private mapRemoves: Node[] = [];

private movedMap: Record<string, true> = {};
Expand Down Expand Up @@ -353,7 +354,7 @@ export default class MutationBuffer {

for (const n of this.movedSet) {
if (
isParentRemoved(this.removes, n, this.mirror) &&
isParentRemoved(this.removesMap, n, this.mirror) &&
!this.movedSet.has(n.parentNode!)
) {
continue;
Expand All @@ -364,7 +365,7 @@ export default class MutationBuffer {
for (const n of this.addedSet) {
if (
!isAncestorInSet(this.droppedSet, n) &&
!isParentRemoved(this.removes, n, this.mirror)
!isParentRemoved(this.removesMap, n, this.mirror)
) {
pushAdd(n);
} else if (isAncestorInSet(this.movedSet, n)) {
Expand Down Expand Up @@ -503,6 +504,7 @@ export default class MutationBuffer {
this.attributes = [];
this.attributeMap = new WeakMap<Node, attributeCursor>();
this.removes = [];
this.removesMap = new Map<number, number>();
this.addedSet = new Set<Node>();
this.movedSet = new Set<Node>();
this.droppedSet = new Set<Node>();
Expand Down Expand Up @@ -726,6 +728,7 @@ export default class MutationBuffer {
? true
: undefined,
});
this.removesMap.set(nodeId, this.removes.length - 1);
}
this.mapRemoves.push(n);
});
Expand Down Expand Up @@ -789,16 +792,16 @@ function deepDelete(addsSet: Set<Node>, n: Node) {
}

function isParentRemoved(
removes: removedNodeMutation[],
removesMap: Map<number, number>,
n: Node,
mirror: Mirror,
): boolean {
if (removes.length === 0) return false;
return _isParentRemoved(removes, n, mirror);
if (removesMap.size === 0) return false;
return _isParentRemoved(removesMap, n, mirror);
}

function _isParentRemoved(
removes: removedNodeMutation[],
removesMap: Map<number, number>,
n: Node,
mirror: Mirror,
): boolean {
Expand All @@ -807,10 +810,10 @@ function _isParentRemoved(
return false;
}
const parentId = mirror.getId(parentNode);
if (removes.some((r) => r.id === parentId)) {
if (removesMap.has(parentId)) {
return true;
}
return _isParentRemoved(removes, parentNode, mirror);
return _isParentRemoved(removesMap, parentNode, mirror);
}

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

0 comments on commit a9fc91a

Please sign in to comment.