From c68b3f26d7e58ca1dfe2c0061981f36fb4a470fe Mon Sep 17 00:00:00 2001 From: Michael Dellanoce Date: Wed, 6 Dec 2023 11:28:15 -0500 Subject: [PATCH] optimize isParentRemoved for large remove lists --- .changeset/late-keys-whisper.md | 5 +++++ packages/rrweb/src/record/mutation.ts | 16 ++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) create mode 100644 .changeset/late-keys-whisper.md diff --git a/.changeset/late-keys-whisper.md b/.changeset/late-keys-whisper.md new file mode 100644 index 0000000000..daf97ae1dc --- /dev/null +++ b/.changeset/late-keys-whisper.md @@ -0,0 +1,5 @@ +--- +'rrweb': patch +--- + +Optimize isParentRemoved for large remove lists diff --git a/packages/rrweb/src/record/mutation.ts b/packages/rrweb/src/record/mutation.ts index f4267af340..33a8861b2b 100644 --- a/packages/rrweb/src/record/mutation.ts +++ b/packages/rrweb/src/record/mutation.ts @@ -143,7 +143,7 @@ export default class MutationBuffer { private texts: textCursor[] = []; private attributes: attributeCursor[] = []; private attributeMap = new WeakMap(); - private removes: removedNodeMutation[] = []; + private removes = new Map(); private mapRemoves: Node[] = []; private movedMap: Record = {}; @@ -485,7 +485,7 @@ export default class MutationBuffer { .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 @@ -502,7 +502,7 @@ export default class MutationBuffer { this.texts = []; this.attributes = []; this.attributeMap = new WeakMap(); - this.removes = []; + this.removes = new Map(); this.addedSet = new Set(); this.movedSet = new Set(); this.droppedSet = new Set(); @@ -718,7 +718,7 @@ export default class MutationBuffer { ) { deepDelete(this.movedSet, n); } else { - this.removes.push({ + this.removes.set(nodeId, { parentId, id: nodeId, isShadow: @@ -789,16 +789,16 @@ function deepDelete(addsSet: Set, n: Node) { } function isParentRemoved( - removes: removedNodeMutation[], + removes: Map, 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, n: Node, mirror: Mirror, ): boolean { @@ -807,7 +807,7 @@ function _isParentRemoved( 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);