From 31297557cfaa11a8c7ccc9364249adfaff167a79 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 | 19 +++++++++++-------- 2 files changed, 16 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 d08c2c7892..083b878d0d 100644 --- a/packages/rrweb/src/record/mutation.ts +++ b/packages/rrweb/src/record/mutation.ts @@ -144,6 +144,7 @@ export default class MutationBuffer { private attributes: attributeCursor[] = []; private attributeMap = new WeakMap(); private removes: removedNodeMutation[] = []; + private removesMap = new Map(); private mapRemoves: Node[] = []; private movedMap: Record = {}; @@ -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; @@ -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)) { @@ -498,6 +499,7 @@ export default class MutationBuffer { this.attributes = []; this.attributeMap = new WeakMap(); this.removes = []; + this.removesMap = new Map(); this.addedSet = new Set(); this.movedSet = new Set(); this.droppedSet = new Set(); @@ -709,6 +711,7 @@ export default class MutationBuffer { ? true : undefined, }); + this.removesMap.set(nodeId, this.removes.length - 1); } this.mapRemoves.push(n); }); @@ -778,16 +781,16 @@ function deepDelete(addsSet: Set, n: Node) { } function isParentRemoved( - removes: removedNodeMutation[], + removesMap: Map, 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, n: Node, mirror: Mirror, ): boolean { @@ -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, n: Node): boolean {