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: improve serialize text #1537

Closed
wants to merge 7 commits into from
Closed
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/shaggy-bags-sneeze.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"rrweb": patch
---

optimize attribute serialization
45 changes: 28 additions & 17 deletions packages/rrweb/src/record/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import type {
removedNodeMutation,
addedNodeMutation,
Optional,
mutationCallbackParam,
} from '@rrweb/types';
import {
isBlocked,
Expand Down Expand Up @@ -260,6 +261,32 @@ export default class MutationBuffer {
this.emit(); // clears buffer if not locked/frozen
};

private serializeTexts(
input: textCursor[],
addedIds: Set<number>,
): mutationCallbackParam['texts'] {
const texts = [];
for (let i = 0; i < input.length; i++) {
const n = input[i].node;
const parent = dom.parentNode(n);
if (parent && (parent as Element).tagName === 'TEXTAREA') {
// the node is being ignored as it isn't in the mirror, so shift mutation to attributes on parent textarea
this.genTextAreaValueMutation(parent as HTMLTextAreaElement);
}

const id = this.mirror.getId(n);
if (addedIds.has(id) || !this.mirror.has(id)) {
continue;
}
texts.push({
id,
value: input[i].value,
});
}

return texts;
}

public emit = () => {
if (this.frozen || this.locked) {
return;
Expand Down Expand Up @@ -448,23 +475,7 @@ export default class MutationBuffer {
}

const payload = {
texts: this.texts
.map((text) => {
const n = text.node;
const parent = dom.parentNode(n);
if (parent && (parent as Element).tagName === 'TEXTAREA') {
// the node is being ignored as it isn't in the mirror, so shift mutation to attributes on parent textarea
this.genTextAreaValueMutation(parent as HTMLTextAreaElement);
}
return {
id: this.mirror.getId(n),
value: text.value,
};
})
// no need to include them on added elements, as they have just been serialized with up to date attribubtes
.filter((text) => !addedIds.has(text.id))
// text mutation's id was not in the mirror map means the target node has been removed
.filter((text) => this.mirror.has(text.id)),
texts: this.serializeTexts(this.texts, addedIds),
attributes: this.attributes
.map((attribute) => {
const { attributes } = attribute;
Expand Down
Loading