perf(snapshot): avoid recreate element a every time (#1387)
perf(snapshot): avoid costly generation of <a> element on each call to `getHref`, instead cache an anchor element and reuse it's href attributed --------- Co-authored-by: Eoghan Murray <eoghan@getthere.ie>
This commit is contained in:
committed by
GitHub
parent
f1e6a51dc6
commit
5e7943dbae
10
.changeset/hungry-dodos-taste.md
Normal file
10
.changeset/hungry-dodos-taste.md
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
---
|
||||||
|
'rrweb-snapshot': patch
|
||||||
|
---
|
||||||
|
|
||||||
|
Avoid recreating the same element every time, instead, we cache and we just update the element.
|
||||||
|
|
||||||
|
Before: 779k ops/s
|
||||||
|
After: 860k ops/s
|
||||||
|
|
||||||
|
Benchmark: https://jsbench.me/ktlqztuf95/1
|
||||||
@@ -196,23 +196,31 @@ function getAbsoluteSrcsetString(doc: Document, attributeValue: string) {
|
|||||||
return output.join(', ');
|
return output.join(', ');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const cachedDocument = new WeakMap<Document, HTMLAnchorElement>();
|
||||||
|
|
||||||
export function absoluteToDoc(doc: Document, attributeValue: string): string {
|
export function absoluteToDoc(doc: Document, attributeValue: string): string {
|
||||||
if (!attributeValue || attributeValue.trim() === '') {
|
if (!attributeValue || attributeValue.trim() === '') {
|
||||||
return attributeValue;
|
return attributeValue;
|
||||||
}
|
}
|
||||||
const a: HTMLAnchorElement = doc.createElement('a');
|
|
||||||
a.href = attributeValue;
|
return getHref(doc, attributeValue);
|
||||||
return a.href;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function isSVGElement(el: Element): boolean {
|
function isSVGElement(el: Element): boolean {
|
||||||
return Boolean(el.tagName === 'svg' || (el as SVGElement).ownerSVGElement);
|
return Boolean(el.tagName === 'svg' || (el as SVGElement).ownerSVGElement);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getHref() {
|
function getHref(doc: Document, customHref?: string) {
|
||||||
// return a href without hash
|
let a = cachedDocument.get(doc);
|
||||||
const a = document.createElement('a');
|
if (!a) {
|
||||||
a.href = '';
|
a = doc.createElement('a');
|
||||||
|
cachedDocument.set(doc, a);
|
||||||
|
}
|
||||||
|
if (!customHref) {
|
||||||
|
customHref = '';
|
||||||
|
}
|
||||||
|
// note: using `new URL` is slower. See #1434 or https://jsbench.me/uqlud17rxo/1
|
||||||
|
a.setAttribute('href', customHref);
|
||||||
return a.href;
|
return a.href;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -244,7 +252,7 @@ export function transformAttribute(
|
|||||||
} else if (name === 'srcset') {
|
} else if (name === 'srcset') {
|
||||||
return getAbsoluteSrcsetString(doc, value);
|
return getAbsoluteSrcsetString(doc, value);
|
||||||
} else if (name === 'style') {
|
} else if (name === 'style') {
|
||||||
return absoluteToStylesheet(value, getHref());
|
return absoluteToStylesheet(value, getHref(doc));
|
||||||
} else if (tagName === 'object' && name === 'data') {
|
} else if (tagName === 'object' && name === 'data') {
|
||||||
return absoluteToDoc(doc, value);
|
return absoluteToDoc(doc, value);
|
||||||
}
|
}
|
||||||
@@ -506,6 +514,7 @@ function serializeNode(
|
|||||||
});
|
});
|
||||||
case n.TEXT_NODE:
|
case n.TEXT_NODE:
|
||||||
return serializeTextNode(n as Text, {
|
return serializeTextNode(n as Text, {
|
||||||
|
doc,
|
||||||
needsMask,
|
needsMask,
|
||||||
maskTextFn,
|
maskTextFn,
|
||||||
rootId,
|
rootId,
|
||||||
@@ -536,6 +545,7 @@ function getRootId(doc: Document, mirror: Mirror): number | undefined {
|
|||||||
function serializeTextNode(
|
function serializeTextNode(
|
||||||
n: Text,
|
n: Text,
|
||||||
options: {
|
options: {
|
||||||
|
doc: Document;
|
||||||
needsMask: boolean | undefined;
|
needsMask: boolean | undefined;
|
||||||
maskTextFn: MaskTextFn | undefined;
|
maskTextFn: MaskTextFn | undefined;
|
||||||
rootId: number | undefined;
|
rootId: number | undefined;
|
||||||
@@ -567,7 +577,7 @@ function serializeTextNode(
|
|||||||
n,
|
n,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
textContent = absoluteToStylesheet(textContent, getHref());
|
textContent = absoluteToStylesheet(textContent, getHref(options.doc));
|
||||||
}
|
}
|
||||||
if (isScript) {
|
if (isScript) {
|
||||||
textContent = 'SCRIPT_PLACEHOLDER';
|
textContent = 'SCRIPT_PLACEHOLDER';
|
||||||
@@ -661,7 +671,7 @@ function serializeElementNode(
|
|||||||
(n as HTMLStyleElement).sheet as CSSStyleSheet,
|
(n as HTMLStyleElement).sheet as CSSStyleSheet,
|
||||||
);
|
);
|
||||||
if (cssText) {
|
if (cssText) {
|
||||||
attributes._cssText = absoluteToStylesheet(cssText, getHref());
|
attributes._cssText = absoluteToStylesheet(cssText, getHref(doc));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// form fields
|
// form fields
|
||||||
|
|||||||
Reference in New Issue
Block a user