From a95c735d24edac57f827e17c7a52ca3c9f8bc229 Mon Sep 17 00:00:00 2001 From: Michael Dellanoce Date: Thu, 7 Sep 2023 13:48:13 -0400 Subject: [PATCH] add benchmark and optimize addList worst case performance --- packages/rrweb/src/record/mutation.ts | 11 +++++++ .../rrweb/test/benchmark/dom-mutation.test.ts | 28 ++++++++++++++++ .../benchmark-dom-mutation-add-reorder.html | 33 +++++++++++++++++++ 3 files changed, 72 insertions(+) create mode 100644 packages/rrweb/test/html/benchmark-dom-mutation-add-reorder.html diff --git a/packages/rrweb/src/record/mutation.ts b/packages/rrweb/src/record/mutation.ts index 097d1a8fd5..9fde0c5fd9 100644 --- a/packages/rrweb/src/record/mutation.ts +++ b/packages/rrweb/src/record/mutation.ts @@ -128,6 +128,16 @@ class DoubleLinkedList { } this.length--; } + + public defragment() { + let current = this.head; + while (current) { + let next = current.next; + this.removeNode(current.value); + this.addNode(current.value); + current = next; + } + } } const moveKey = (id: number, parentId: number) => `${id}@${parentId}`; @@ -368,6 +378,7 @@ export default class MutationBuffer { } let candidate: DoubleLinkedListNode | null = null; + addList.defragment() while (addList.length) { let node: DoubleLinkedListNode | null = null; if (candidate) { diff --git a/packages/rrweb/test/benchmark/dom-mutation.test.ts b/packages/rrweb/test/benchmark/dom-mutation.test.ts index 3da794db45..b81932fea0 100644 --- a/packages/rrweb/test/benchmark/dom-mutation.test.ts +++ b/packages/rrweb/test/benchmark/dom-mutation.test.ts @@ -42,6 +42,34 @@ const suites: Array< eval: 'window.workload()', times: 5, }, +<<<<<<< Updated upstream +======= + { + title: 'mask 1000x10 DOM nodes', + html: 'benchmark-text-masking.html', + eval: 'window.workload()', + times: 10, + recordOptions: { + maskTextClass: 'rr-mask', + }, + }, + { + title: 'unmask 1000x10 DOM nodes', + html: 'benchmark-text-masking.html', + eval: 'window.workload()', + times: 10, + recordOptions: { + maskTextClass: 'rr-mask', + unmaskTextClass: 'rr-unmask', + }, + }, + { + title: 'add and reorder 10000 DOM nodes', + html: 'benchmark-dom-mutation-add-reorder.html', + eval: 'window.workload()', + times: 5, + }, +>>>>>>> Stashed changes ]; function avg(v: number[]): number { diff --git a/packages/rrweb/test/html/benchmark-dom-mutation-add-reorder.html b/packages/rrweb/test/html/benchmark-dom-mutation-add-reorder.html new file mode 100644 index 0000000000..c4b7d24d11 --- /dev/null +++ b/packages/rrweb/test/html/benchmark-dom-mutation-add-reorder.html @@ -0,0 +1,33 @@ + + + + + +