-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
fix(perf): improve perf of mutation and snapshot #1271
Closed
Closed
Changes from all commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
cc90576
perf(snapshot): optimize code flow of slimDomExcluded
JonasBa 18ca335
perf(snapshot): remove lowerIfExists
JonasBa ba66b77
perf(snapshot): return early
JonasBa dc4c7b1
perf(snapshot): test
JonasBa 85b23bf
perf(mutation): shift n^2
JonasBa bad990b
perf(mutation): improve process
JonasBa 5d59621
fix(mutation): revert regexp exec
JonasBa 58ab15c
fix(prettier): apply formatting
JonasBa 011e0d0
Merge branch 'master' into jb/perf/replay
JonasBa f6c8738
fix: filter condition
JonasBa e6bb17e
fix: remove unused import
JonasBa a927384
fix: lint
JonasBa 23512e6
test: update snapshots
JonasBa 510d82d
revert(genAdds): revert queue implementation
JonasBa fab0d3d
revert(genAdds): revert snapshot
JonasBa e188253
feat(benchmark): add blockClass and blockSelector benchmarks
JonasBa 12dc788
Merge branch 'master' into jb/perf/replay
JonasBa 8750f7f
Revert "feat(benchmark): add blockClass and blockSelector benchmarks"
JonasBa 5b05e6b
ref(perf): less recursion and a few minor optimizations
JonasBa 074fdd9
Revert "ref(perf): less recursion and a few minor optimizations"
JonasBa eab4b25
ref(perf): less recursion
JonasBa eed46b0
fix(lint): format doc
JonasBa bc2c8f2
Merge branch 'master' into jb/perf/replay
JonasBa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -262,6 +262,10 @@ export function _isBlockedElement( | |
blockClass: string | RegExp, | ||
blockSelector: string | null, | ||
): boolean { | ||
if (!blockClass && !blockSelector) { | ||
return false; | ||
} | ||
|
||
try { | ||
if (typeof blockClass === 'string') { | ||
if (element.classList.contains(blockClass)) { | ||
|
@@ -311,23 +315,27 @@ export function needMaskingText( | |
maskTextClass: string | RegExp, | ||
maskTextSelector: string | null, | ||
): boolean { | ||
try { | ||
const el: HTMLElement | null = | ||
node.nodeType === node.ELEMENT_NODE | ||
? (node as HTMLElement) | ||
: node.parentElement; | ||
if (el === null) return false; | ||
if (!maskTextClass && !maskTextSelector) { | ||
return false; | ||
} | ||
|
||
const el: HTMLElement | null = | ||
node.nodeType === node.ELEMENT_NODE | ||
? (node as HTMLElement) | ||
: node.parentElement; | ||
|
||
if (el === null) return false; | ||
try { | ||
if (typeof maskTextClass === 'string') { | ||
if (el.classList.contains(maskTextClass)) return true; | ||
if (el.closest(`.${maskTextClass}`)) return true; | ||
if (el.matches(`.${maskTextClass} *`)) return true; | ||
} else { | ||
if (classMatchesRegex(el, maskTextClass, true)) return true; | ||
} | ||
|
||
if (maskTextSelector) { | ||
if (el.matches(maskTextSelector)) return true; | ||
if (el.closest(maskTextSelector)) return true; | ||
if (el.matches(`${maskTextSelector} *`)) return true; | ||
} | ||
} catch (e) { | ||
// | ||
|
@@ -541,8 +549,8 @@ function serializeTextNode( | |
// The parent node may not be a html element which has a tagName attribute. | ||
// So just let it be undefined which is ok in this use case. | ||
const parentTagName = n.parentNode && (n.parentNode as HTMLElement).tagName; | ||
let textContent = n.textContent; | ||
const isStyle = parentTagName === 'STYLE' ? true : undefined; | ||
let textContent = n.textContent; | ||
const isScript = parentTagName === 'SCRIPT' ? true : undefined; | ||
if (isStyle && textContent) { | ||
try { | ||
|
@@ -568,15 +576,22 @@ function serializeTextNode( | |
if (isScript) { | ||
textContent = 'SCRIPT_PLACEHOLDER'; | ||
} | ||
|
||
if ( | ||
!isStyle && | ||
!isScript && | ||
textContent && | ||
needMaskingText(n, maskTextClass, maskTextSelector) | ||
) { | ||
textContent = maskTextFn | ||
? maskTextFn(textContent) | ||
: textContent.replace(/[\S]/g, '*'); | ||
return { | ||
type: NodeType.Text, | ||
textContent: | ||
(maskTextFn | ||
? maskTextFn(textContent) | ||
: textContent.replace(/[\S]/g, '*')) || '', | ||
isStyle, | ||
rootId, | ||
}; | ||
} | ||
|
||
return { | ||
|
@@ -812,24 +827,40 @@ function serializeElementNode( | |
}; | ||
} | ||
|
||
function lowerIfExists( | ||
maybeAttr: string | number | boolean | undefined | null, | ||
): string { | ||
if (maybeAttr === undefined || maybeAttr === null) { | ||
return ''; | ||
} else { | ||
return (maybeAttr as string).toLowerCase(); | ||
} | ||
} | ||
const MS_APPLICATION_TILE_REGEXP = /^msapplication-tile(image|color)$/; | ||
const OG_TWITTER_OR_FB_REGEXP = /^(og|twitter|fb):/; | ||
const OG_TWITTER_REGEXP = /^(og|twitter):/; | ||
const ARTICLE_PRODUCT_REGEXP = /^(article|product):/; | ||
|
||
function slimDOMExcluded( | ||
sn: serializedNode, | ||
slimDOMOptions: SlimDOMOptions, | ||
): boolean { | ||
if (slimDOMOptions.comment && sn.type === NodeType.Comment) { | ||
if (sn.type !== NodeType.Element && sn.type !== NodeType.Comment) { | ||
return false; | ||
} | ||
|
||
if (sn.type === NodeType.Comment && slimDOMOptions.comment) { | ||
// TODO: convert IE conditional comments to real nodes | ||
return true; | ||
} else if (sn.type === NodeType.Element) { | ||
} | ||
|
||
if (sn.type === NodeType.Element) { | ||
/* eslint-disable */ | ||
const snAttributeName: string = sn.attributes.name | ||
JonasBa marked this conversation as resolved.
Show resolved
Hide resolved
|
||
? // @ts-ignore | ||
sn.attributes.name.toLowerCase() | ||
: ''; | ||
const snAttributeRel: string = sn.attributes.rel | ||
? // @ts-ignore | ||
sn.attributes.rel.toLowerCase() | ||
: ''; | ||
const snAttributeProperty: string = sn.attributes.property | ||
? // @ts-ignore | ||
sn.attributes.property.toLowerCase() | ||
: ''; | ||
/* eslint-enable */ | ||
|
||
if ( | ||
slimDOMOptions.script && | ||
// script tag | ||
|
@@ -850,33 +881,30 @@ function slimDOMExcluded( | |
slimDOMOptions.headFavicon && | ||
((sn.tagName === 'link' && sn.attributes.rel === 'shortcut icon') || | ||
(sn.tagName === 'meta' && | ||
(lowerIfExists(sn.attributes.name).match( | ||
/^msapplication-tile(image|color)$/, | ||
) || | ||
lowerIfExists(sn.attributes.name) === 'application-name' || | ||
lowerIfExists(sn.attributes.rel) === 'icon' || | ||
lowerIfExists(sn.attributes.rel) === 'apple-touch-icon' || | ||
lowerIfExists(sn.attributes.rel) === 'shortcut icon'))) | ||
(snAttributeName === 'application-name' || | ||
snAttributeRel === 'icon' || | ||
snAttributeRel === 'apple-touch-icon' || | ||
snAttributeRel === 'shortcut icon' || | ||
MS_APPLICATION_TILE_REGEXP.test(snAttributeName)))) | ||
) { | ||
return true; | ||
} else if (sn.tagName === 'meta') { | ||
if ( | ||
slimDOMOptions.headMetaDescKeywords && | ||
lowerIfExists(sn.attributes.name).match(/^description|keywords$/) | ||
(snAttributeName === 'description' || snAttributeName === 'keywords') | ||
) { | ||
return true; | ||
} else if ( | ||
slimDOMOptions.headMetaSocial && | ||
(lowerIfExists(sn.attributes.property).match(/^(og|twitter|fb):/) || // og = opengraph (facebook) | ||
lowerIfExists(sn.attributes.name).match(/^(og|twitter):/) || | ||
lowerIfExists(sn.attributes.name) === 'pinterest') | ||
(OG_TWITTER_OR_FB_REGEXP.test(snAttributeProperty) || // og = opengraph (facebook) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. very tiny nitpick: that the comment makes more sense up at the declaration now 🙈 |
||
OG_TWITTER_REGEXP.test(snAttributeName)) | ||
) { | ||
return true; | ||
} else if ( | ||
slimDOMOptions.headMetaRobots && | ||
(lowerIfExists(sn.attributes.name) === 'robots' || | ||
lowerIfExists(sn.attributes.name) === 'googlebot' || | ||
lowerIfExists(sn.attributes.name) === 'bingbot') | ||
(snAttributeName === 'robots' || | ||
snAttributeName === 'googlebot' || | ||
snAttributeName === 'bingbot') | ||
) { | ||
return true; | ||
} else if ( | ||
|
@@ -888,24 +916,23 @@ function slimDOMExcluded( | |
return true; | ||
} else if ( | ||
slimDOMOptions.headMetaAuthorship && | ||
(lowerIfExists(sn.attributes.name) === 'author' || | ||
lowerIfExists(sn.attributes.name) === 'generator' || | ||
lowerIfExists(sn.attributes.name) === 'framework' || | ||
lowerIfExists(sn.attributes.name) === 'publisher' || | ||
lowerIfExists(sn.attributes.name) === 'progid' || | ||
lowerIfExists(sn.attributes.property).match(/^article:/) || | ||
lowerIfExists(sn.attributes.property).match(/^product:/)) | ||
(snAttributeName === 'author' || | ||
snAttributeName === 'generator' || | ||
snAttributeName === 'framework' || | ||
snAttributeName === 'publisher' || | ||
snAttributeName === 'progid' || | ||
ARTICLE_PRODUCT_REGEXP.test(snAttributeProperty)) | ||
) { | ||
return true; | ||
} else if ( | ||
slimDOMOptions.headMetaVerification && | ||
(lowerIfExists(sn.attributes.name) === 'google-site-verification' || | ||
lowerIfExists(sn.attributes.name) === 'yandex-verification' || | ||
lowerIfExists(sn.attributes.name) === 'csrf-token' || | ||
lowerIfExists(sn.attributes.name) === 'p:domain_verify' || | ||
lowerIfExists(sn.attributes.name) === 'verify-v1' || | ||
lowerIfExists(sn.attributes.name) === 'verification' || | ||
lowerIfExists(sn.attributes.name) === 'shopify-checkout-api-token') | ||
(snAttributeName === 'google-site-verification' || | ||
snAttributeName === 'yandex-verification' || | ||
snAttributeName === 'csrf-token' || | ||
snAttributeName === 'p:domain_verify' || | ||
snAttributeName === 'verify-v1' || | ||
snAttributeName === 'verification' || | ||
snAttributeName === 'shopify-checkout-api-token') | ||
) { | ||
return true; | ||
} | ||
|
@@ -1045,6 +1072,7 @@ export function serializeNodeWithId( | |
) { | ||
preserveWhiteSpace = false; | ||
} | ||
|
||
const bypassOptions = { | ||
doc, | ||
mirror, | ||
|
@@ -1069,22 +1097,24 @@ export function serializeNodeWithId( | |
stylesheetLoadTimeout, | ||
keepIframeSrcFn, | ||
}; | ||
for (const childN of Array.from(n.childNodes)) { | ||
|
||
n.childNodes.forEach((childN) => { | ||
const serializedChildNode = serializeNodeWithId(childN, bypassOptions); | ||
if (serializedChildNode) { | ||
serializedNode.childNodes.push(serializedChildNode); | ||
} | ||
} | ||
}); | ||
|
||
if (isElement(n) && n.shadowRoot) { | ||
for (const childN of Array.from(n.shadowRoot.childNodes)) { | ||
n.shadowRoot.childNodes.forEach((childN) => { | ||
const serializedChildNode = serializeNodeWithId(childN, bypassOptions); | ||
if (serializedChildNode) { | ||
isNativeShadowDom(n.shadowRoot) && | ||
(serializedChildNode.isShadow = true); | ||
if (isNativeShadowDom(n.shadowRoot!)) { | ||
serializedChildNode.isShadow = true; | ||
} | ||
serializedNode.childNodes.push(serializedChildNode); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🙌