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 attributes #1538

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
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/large-falcons-smash.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"rrweb": minor
---

improve perf of attribute serialization
68 changes: 40 additions & 28 deletions packages/rrweb/src/record/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
removedNodeMutation,
addedNodeMutation,
Optional,
mutationCallbackParam,
} from '@rrweb/types';
import {
isBlocked,
Expand Down Expand Up @@ -260,6 +261,44 @@
this.emit(); // clears buffer if not locked/frozen
};

private serializeAttributes(
input: attributeCursor[],
addedIds: Set<number>,
): mutationCallbackParam['attributes'] {
const attributes = [];
for (let i = 0; i < input.length; i++) {
const id = this.mirror.getId(input[i].node);
if (addedIds.has(id) || !this.mirror.has(id)) {
continue;
}

const { attributes: elAttributes } = input[i];
if (typeof elAttributes.style === 'string') {
const diffAsStr = JSON.stringify(input[i].styleDiff);
const unchangedAsStr = JSON.stringify(input[i]._unchangedStyles);
// check if the style diff is actually shorter than the regular string based mutation
// (which was the whole point of #464 'compact style mutation').
if (diffAsStr.length < elAttributes.style.length) {
// also: CSSOM fails badly when var() is present on shorthand properties, so only proceed with
// the compact style mutation if these have all been accounted for
if (
(diffAsStr + unchangedAsStr).split('var(').length ===
elAttributes.style.split('var(').length
) {
elAttributes.style = input[i].styleDiff;
}
}
}

attributes.push({
id,
attributes: input[i].attributes,
});
}

return attributes;
}

public emit = () => {
if (this.frozen || this.locked) {
return;
Expand Down Expand Up @@ -362,13 +401,13 @@
};

while (this.mapRemoves.length) {
this.mirror.removeNodeFromMap(this.mapRemoves.shift()!);

Check warning on line 404 in packages/rrweb/src/record/mutation.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/rrweb/src/record/mutation.ts#L404

[@typescript-eslint/no-non-null-assertion] Forbidden non-null assertion.
}

for (const n of this.movedSet) {
if (
isParentRemoved(this.removes, n, this.mirror) &&
!this.movedSet.has(dom.parentNode(n)!)

Check warning on line 410 in packages/rrweb/src/record/mutation.ts

View workflow job for this annotation

GitHub Actions / ESLint Report Analysis

packages/rrweb/src/record/mutation.ts#L410

[@typescript-eslint/no-non-null-assertion] Forbidden non-null assertion.
) {
continue;
}
Expand Down Expand Up @@ -465,34 +504,7 @@
.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)),
attributes: this.attributes
.map((attribute) => {
const { attributes } = attribute;
if (typeof attributes.style === 'string') {
const diffAsStr = JSON.stringify(attribute.styleDiff);
const unchangedAsStr = JSON.stringify(attribute._unchangedStyles);
// check if the style diff is actually shorter than the regular string based mutation
// (which was the whole point of #464 'compact style mutation').
if (diffAsStr.length < attributes.style.length) {
// also: CSSOM fails badly when var() is present on shorthand properties, so only proceed with
// the compact style mutation if these have all been accounted for
if (
(diffAsStr + unchangedAsStr).split('var(').length ===
attributes.style.split('var(').length
) {
attributes.style = attribute.styleDiff;
}
}
}
return {
id: this.mirror.getId(attribute.node),
attributes: attributes,
};
})
// no need to include them on added elements, as they have just been serialized with up to date attribubtes
.filter((attribute) => !addedIds.has(attribute.id))
// attribute mutation's id was not in the mirror map means the target node has been removed
.filter((attribute) => this.mirror.has(attribute.id)),
attributes: this.serializeAttributes(this.attributes, addedIds),
removes: this.removes,
adds,
};
Expand Down
Loading