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

perf: optimize performance of the DoubleLinkedList get #1220

Merged
merged 2 commits into from
May 22, 2023
Merged
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/gold-terms-look.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'rrweb': patch
---

perf: optimize performance of the DoubleLinkedList get
14 changes: 12 additions & 2 deletions packages/rrweb/src/record/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ function isNodeInLinkedList(n: Node | NodeInLinkedList): n is NodeInLinkedList {
class DoubleLinkedList {
public length = 0;
public head: DoubleLinkedListNode | null = null;
public tail: DoubleLinkedListNode | null = null;

public get(position: number) {
if (position >= this.length) {
Expand Down Expand Up @@ -95,6 +96,9 @@ class DoubleLinkedList {
node.next = this.head;
this.head = node;
}
if (node.next === null) {
this.tail = node;
}
this.length++;
}

Expand All @@ -108,11 +112,15 @@ class DoubleLinkedList {
this.head = current.next;
if (this.head) {
this.head.previous = null;
} else {
this.tail = null;
}
} else {
current.previous.next = current.next;
if (current.next) {
current.next.previous = current.previous;
} else {
this.tail = current.previous;
}
}
if (n.__ln) {
Expand Down Expand Up @@ -368,8 +376,10 @@ export default class MutationBuffer {
}
}
if (!node) {
for (let index = addList.length - 1; index >= 0; index--) {
const _node = addList.get(index);
let tailNode = addList.tail;
while (tailNode) {
const _node = tailNode;
tailNode = tailNode.previous;
// ensure _node is defined before attempting to find value
if (_node) {
const parentId = this.mirror.getId(_node.value.parentNode);
Expand Down
6 changes: 6 additions & 0 deletions packages/rrweb/test/benchmark/dom-mutation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ const suites: Array<
eval: 'window.workload()',
times: 5,
},
{
title: 'create 10000 DOM nodes and move it to new container',
html: 'benchmark-dom-mutation-add-and-move.html',
eval: 'window.workload()',
times: 5,
},
];

function avg(v: number[]): number {
Expand Down
37 changes: 37 additions & 0 deletions packages/rrweb/test/html/benchmark-dom-mutation-add-and-move.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<html>
<body>
<div id="app"></div>
</body>
<script>
function appendNodes(parentNode, total) {
const el = document.createElement('div');
for (let i = 0; i < total; i++) {
const div = document.createElement('div');
div.innerHTML = `div-${i + 1}`;
el.append(div);
}
parentNode.append(el);
return el;
}

function addAndMove() {
const app = document.getElementById('app');

const elContainer = appendNodes(app, 10000);

const newAppContainer = document.createElement('div');

newAppContainer.append(elContainer);

document.body.append(newAppContainer);

app.remove();

// necessary
appendNodes(document.body, 1);
}
window.workload = () => {
addAndMove();
};
</script>
</html>