-
Notifications
You must be signed in to change notification settings - Fork 426
perf(template-compiler): optimize all static nodes #2969
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
Conversation
|
The tests are passing in all browsers, and as a reminder, we have a Karma test for static lwc/packages/@lwc/integration-karma/test/static-content/index.spec.js Lines 172 to 186 in dba53e7
|
| const template = document.createElement('template'); | ||
| template.innerHTML = html; | ||
| return template.content.firstChild; | ||
| }; |
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.
I considered caching and re-using the template here, but I decided against it because:
- This is just a one-time setup cost anyway – it wouldn't improve any of our benchmarks
- Holding onto the memory from the last HTML in the cached
templatestrikes me as a little weird - I'm not even sure if it's a big perf improvement in the first place
We might explore caching the template in a follow-up PR, though.
|
/nucleus test |
|
|
||
| // Via https://github.com/webcomponents/polyfills/blob/ee1db33/packages/template/template.js#L273-L280 | ||
| const topLevelWrappingMap: { [key: string]: string[] } = { | ||
| option: ['select'], |
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.
does tfoot, colgroup, tbody and caption also need to be on this map? (I don't think you can add head, body and html on an lwc template)
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.
Nice catch! Fixed and added a test to make sure it actually works in IE11. I did not include head/body/html.
| let content: Node = doc.body; | ||
| if (!isUndefined(wrapperTags)) { | ||
| for (let i = 0; i < wrapperTags.length; i++) { | ||
| content = content.lastChild!; |
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.
why we use .lastChild here, and .firstChild at the end?
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.
That was copypasta from webcomponents/polyfills here, but I think you're right; we can just use firstChild which is less confusing.
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.
Fixed, switched to firstChild.
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.
🥇 ![]()
|
|
||
| let content: Node = doc.body; | ||
| if (!isUndefined(wrapperTags)) { | ||
| for (let i = 0; i < wrapperTags.length; i++) { |
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.
| for (let i = 0; i < wrapperTags.length; i++) { | |
| for (let i = 0, n = wrapperTags.length; i < n; i++) { |
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.
Last I checked, JS engines have optimized this; there's no need to cache the length.
There is some discussion on SO, although sadly all the JSPerf links are dead now. 😢
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.
OK, found a post from a V8 engineer; we do not need to cache length. 😁
Details
Fixes #2890.
Rather than avoiding unsafe top-level elements like
<td>and<th>, we can use a<template>for parsing these. This allows us to extend the static optimization to more elements.To support IE11, we can fall back to a partial implementation of the
<template>element borrowed fromwebcomponents/polyfills. So this PR is not dropping IE11 support.Does this pull request introduce a breaking change?
Does this pull request introduce an observable change?