Skip to content

Commit

Permalink
perf: improve blocked and masking logic checks
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasBa committed Jul 18, 2024
1 parent 40bbc25 commit 93e65f3
Show file tree
Hide file tree
Showing 10 changed files with 30 additions and 35 deletions.
3 changes: 2 additions & 1 deletion packages/rrweb/src/record/mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,9 @@ export default class MutationBuffer {
}
};


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

for (const n of this.movedSet) {
Expand Down
5 changes: 2 additions & 3 deletions packages/rrweb/src/record/processed-node-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,8 @@ export default class ProcessedNodeManager {

public inOtherBuffer(node: Node, thisBuffer: MutationBuffer) {
const buffers = this.nodeMap.get(node);
return (
buffers && Array.from(buffers).some((buffer) => buffer !== thisBuffer)
);
if (!buffers) return false;
return buffers.has(thisBuffer);
}

public add(node: Node, buffer: MutationBuffer) {
Expand Down
29 changes: 11 additions & 18 deletions packages/rrweb/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -246,29 +246,22 @@ export function isBlocked(
blockSelector: string | null,
checkAncestors: boolean,
): boolean {
if (!node) {
return false;
}
const el = closestElementOfNode(node);
if (!el) return false;
if (!blockClass && !blockSelector) return false;

if (!el) {
return false;
}

try {
if (typeof blockClass === 'string') {
if (el.classList.contains(blockClass)) return true;
if (checkAncestors && el.closest('.' + blockClass) !== null) return true;
} else {
if (classMatchesRegex(el, blockClass, checkAncestors)) return true;
}
} catch (e) {
// e
}
if (blockSelector) {
if (el.matches(blockSelector)) return true;
if (checkAncestors && el.closest(blockSelector) !== null) return true;
if (checkAncestors && el.matches(`${blockSelector} *`)) return true;
}

if (typeof blockClass === 'string') {
if (el.matches(`.${blockClass}`)) return true;
if (checkAncestors && el.matches(`.${blockClass} *`)) return true;
} else {
if (classMatchesRegex(el, blockClass, checkAncestors)) return true;
}

return false;
}

Expand Down
22 changes: 9 additions & 13 deletions packages/rrweb/test/benchmark/dom-mutation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,17 +89,6 @@ describe('benchmark: mutation observer', () => {
return fs.readFileSync(filePath, 'utf8');
};

const addRecordingScript = async (page: Page) => {
// const scriptUrl = `${getServerURL(server)}/rrweb-1.1.3.js`;
const scriptUrl = `${getServerURL(server)}/rrweb.umd.cjs`;
await page.evaluate((url) => {
const scriptEl = document.createElement('script');
scriptEl.src = url;
document.head.append(scriptEl);
}, scriptUrl);
await page.waitForFunction('window.rrweb');
};

for (const suite of suites) {
it(suite.title, async () => {
page = await browser.newPage();
Expand All @@ -110,12 +99,19 @@ describe('benchmark: mutation observer', () => {
const loadPage = async () => {
if ('html' in suite) {
await page.goto('about:blank');
const code = fs
.readFileSync(path.resolve(__dirname, '../../dist/rrweb.umd.cjs'))
.toString();
await page.setContent(`<html>
<script>
${code.toString()}
</script>
</html>`);

await page.setContent(getHtml.call(this, suite.html));
} else {
await page.goto(suite.url);
}

await addRecordingScript(page);
};

const getDuration = async (): Promise<number> => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<html>
<head></head>
<body>
<div id="app"></div>
</body>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<html>
<head></head>
<body></body>
<script>
function add() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<html>
<head></head>
<body></body>
<script>
function init() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<html>
<head></head>
<body></body>
<script>
function init() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<html>
<head></head>
<body></body>
<script>
function addMultiNodes() {
Expand Down
1 change: 1 addition & 0 deletions packages/rrweb/test/html/benchmark-dom-mutation.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<html>
<head></head>
<body></body>
<script>
window.workload = () => {
Expand Down

0 comments on commit 93e65f3

Please sign in to comment.