Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

optimize isParentRemoved for large remove lists #1368

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
16 changes: 8 additions & 8 deletions packages/rrweb/src/record/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@
private texts: textCursor[] = [];
private attributes: attributeCursor[] = [];
private attributeMap = new WeakMap<Node, attributeCursor>();
private removes: removedNodeMutation[] = [];
private removes = new Map<number, removedNodeMutation>();
private mapRemoves: Node[] = [];

private movedMap: Record<string, true> = {};
Expand Down Expand Up @@ -348,13 +348,13 @@
};

while (this.mapRemoves.length) {
this.mirror.removeNodeFromMap(this.mapRemoves.shift()!);

Check warning on line 351 in packages/rrweb/src/record/mutation.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/rrweb/src/record/mutation.ts#L351

[@typescript-eslint/no-non-null-assertion] Forbidden non-null assertion.
}

for (const n of this.movedSet) {
if (
isParentRemoved(this.removes, n, this.mirror) &&
!this.movedSet.has(n.parentNode!)

Check warning on line 357 in packages/rrweb/src/record/mutation.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/rrweb/src/record/mutation.ts#L357

[@typescript-eslint/no-non-null-assertion] Forbidden non-null assertion.
) {
continue;
}
Expand Down Expand Up @@ -485,7 +485,7 @@
.filter((attribute) => !addedIds.has(attribute.id))
// attribute mutation's id was not in the mirror map means the target node has been removed
.filter((attribute) => this.mirror.has(attribute.id)),
removes: this.removes,
removes: Array.from(this.removes.values()),
adds,
};
// payload may be empty if the mutations happened in some blocked elements
Expand All @@ -502,7 +502,7 @@
this.texts = [];
this.attributes = [];
this.attributeMap = new WeakMap<Node, attributeCursor>();
this.removes = [];
this.removes = new Map<number, removedNodeMutation>();
this.addedSet = new Set<Node>();
this.movedSet = new Set<Node>();
this.droppedSet = new Set<Node>();
Expand Down Expand Up @@ -718,7 +718,7 @@
) {
deepDelete(this.movedSet, n);
} else {
this.removes.push({
this.removes.set(nodeId, {
parentId,
id: nodeId,
isShadow:
Expand Down Expand Up @@ -789,16 +789,16 @@
}

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

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