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

Performance refactor to avoid 2 function calls #1653

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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/removes-and-descendants.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"rrweb": patch
---

Performance refactor to avoid 2 function calls (bridging between #1543 and #1652)
36 changes: 11 additions & 25 deletions packages/rrweb/src/record/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
isShadowRoot,
needMaskingText,
maskInputValue,
Mirror,
isNativeShadowDom,
getInputType,
toLowerCase,
Expand Down Expand Up @@ -169,7 +168,7 @@
private addedSet = new Set<Node>();
private movedSet = new Set<Node>();
private droppedSet = new Set<Node>();
private removesSubTreeCache = new Set<Node>();
private removesAndDescendants = new Set<Node>();

private mutationCb: observerParam['mutationCb'];
private blockClass: observerParam['blockClass'];
Expand Down Expand Up @@ -363,13 +362,15 @@
};

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

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

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

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

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

for (const n of this.movedSet) {
const movedParent = dom.parentNode(n);
if (
isParentRemoved(this.removesSubTreeCache, n, this.mirror) &&
!this.movedSet.has(dom.parentNode(n)!)
movedParent && // can't be removed if it doesn't exist
this.removesAndDescendants.has(movedParent) &&
!this.movedSet.has(movedParent)
) {
continue;
}
Expand All @@ -379,7 +380,7 @@
for (const n of this.addedSet) {
if (
!isAncestorInSet(this.droppedSet, n) &&
!isParentRemoved(this.removesSubTreeCache, n, this.mirror)
!this.removesAndDescendants.has(dom.parentNode(n)!)

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

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

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

[@typescript-eslint/no-non-null-assertion] Forbidden non-null assertion.
) {
pushAdd(n);
} else if (isAncestorInSet(this.movedSet, n)) {
Expand Down Expand Up @@ -515,7 +516,7 @@
this.addedSet = new Set<Node>();
this.movedSet = new Set<Node>();
this.droppedSet = new Set<Node>();
this.removesSubTreeCache = new Set<Node>();
this.removesAndDescendants = new Set<Node>();
this.movedMap = {};

this.mutationCb(payload);
Expand Down Expand Up @@ -750,7 +751,7 @@
? true
: undefined,
});
processRemoves(n, this.removesSubTreeCache);
populateWithDescendants(n, this.removesAndDescendants);
}
this.mapRemoves.push(n);
});
Expand Down Expand Up @@ -814,35 +815,20 @@
dom.childNodes(n).forEach((childN) => deepDelete(addsSet, childN));
}

function processRemoves(n: Node, cache: Set<Node>) {
function populateWithDescendants(n: Node, s: Set<Node>) {
const queue = [n];

while (queue.length) {
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const next = queue.pop()!;
if (cache.has(next)) continue;
cache.add(next);
if (s.has(next)) continue;
s.add(next);
dom.childNodes(next).forEach((n) => queue.push(n));
}

return;
}

function isParentRemoved(removes: Set<Node>, n: Node, mirror: Mirror): boolean {
if (removes.size === 0) return false;
return _isParentRemoved(removes, n, mirror);
}

function _isParentRemoved(
removes: Set<Node>,
n: Node,
_mirror: Mirror,
): boolean {
const node: ParentNode | null = dom.parentNode(n);
if (!node) return false;
return removes.has(node);
}

function isAncestorInSet(set: Set<Node>, n: Node): boolean {
if (set.size === 0) return false;
return _isAncestorInSet(set, n);
Expand Down
Loading