perf: optimize performance of the DoubleLinkedList get (#1220)
* perf: optimize performance of the DoubleLinkedList get * fix: delete addedNodeIndexArr
This commit is contained in:
5
.changeset/gold-terms-look.md
Normal file
5
.changeset/gold-terms-look.md
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
---
|
||||||
|
'rrweb': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
perf: optimize performance of the DoubleLinkedList get
|
||||||
@@ -48,6 +48,7 @@ function isNodeInLinkedList(n: Node | NodeInLinkedList): n is NodeInLinkedList {
|
|||||||
class DoubleLinkedList {
|
class DoubleLinkedList {
|
||||||
public length = 0;
|
public length = 0;
|
||||||
public head: DoubleLinkedListNode | null = null;
|
public head: DoubleLinkedListNode | null = null;
|
||||||
|
public tail: DoubleLinkedListNode | null = null;
|
||||||
|
|
||||||
public get(position: number) {
|
public get(position: number) {
|
||||||
if (position >= this.length) {
|
if (position >= this.length) {
|
||||||
@@ -95,6 +96,9 @@ class DoubleLinkedList {
|
|||||||
node.next = this.head;
|
node.next = this.head;
|
||||||
this.head = node;
|
this.head = node;
|
||||||
}
|
}
|
||||||
|
if (node.next === null) {
|
||||||
|
this.tail = node;
|
||||||
|
}
|
||||||
this.length++;
|
this.length++;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -108,11 +112,15 @@ class DoubleLinkedList {
|
|||||||
this.head = current.next;
|
this.head = current.next;
|
||||||
if (this.head) {
|
if (this.head) {
|
||||||
this.head.previous = null;
|
this.head.previous = null;
|
||||||
|
} else {
|
||||||
|
this.tail = null;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
current.previous.next = current.next;
|
current.previous.next = current.next;
|
||||||
if (current.next) {
|
if (current.next) {
|
||||||
current.next.previous = current.previous;
|
current.next.previous = current.previous;
|
||||||
|
} else {
|
||||||
|
this.tail = current.previous;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (n.__ln) {
|
if (n.__ln) {
|
||||||
@@ -368,8 +376,10 @@ export default class MutationBuffer {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (!node) {
|
if (!node) {
|
||||||
for (let index = addList.length - 1; index >= 0; index--) {
|
let tailNode = addList.tail;
|
||||||
const _node = addList.get(index);
|
while (tailNode) {
|
||||||
|
const _node = tailNode;
|
||||||
|
tailNode = tailNode.previous;
|
||||||
// ensure _node is defined before attempting to find value
|
// ensure _node is defined before attempting to find value
|
||||||
if (_node) {
|
if (_node) {
|
||||||
const parentId = this.mirror.getId(_node.value.parentNode);
|
const parentId = this.mirror.getId(_node.value.parentNode);
|
||||||
|
|||||||
@@ -36,6 +36,12 @@ const suites: Array<
|
|||||||
eval: 'window.workload()',
|
eval: 'window.workload()',
|
||||||
times: 5,
|
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 {
|
function avg(v: number[]): number {
|
||||||
|
|||||||
@@ -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>
|
||||||
Reference in New Issue
Block a user