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 committed Dec 6, 2023
1 parent 381ef24 commit 3129755
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 @@ -358,7 +359,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 @@ -369,7 +370,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 @@ -498,6 +499,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 @@ -709,6 +711,7 @@ export default class MutationBuffer {
? true
: undefined,
});
this.removesMap.set(nodeId, this.removes.length - 1);
}
this.mapRemoves.push(n);
});
Expand Down Expand Up @@ -778,16 +781,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 @@ -796,10 +799,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 3129755

Please sign in to comment.