-
Notifications
You must be signed in to change notification settings - Fork 395
perf: example rendering performance improvements #961
Conversation
When generating an example in Stackblitz, we have to create a `form` element and submit it to a specific URL. The `form` has to be created eagerly, because some browsers will block us from submitting it if it is created asynchronously. As a result of this setup we fire off a lot of HTTP requests when an example is rendered which slows the page down a lot. These changes make the following improvements which shave off more than a second of scripting time when transitioning from the "Overview" to "Examples". I've used the datepicker examples as a benchmark. 1. Runs the HTTP requests outside of the Angular zone so that we don't trigger change detections once each request is resolved. 2. Caches the file content so that the user doesn't have to load the same file multiple times. I've also fixed that the copyright still said "2020".
} | ||
|
||
Promise.all(templateContents.concat(exampleContents)).then(() => { |
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.
Fun fact: this Promise.all
didn't actually do anything, because the passed-in array was filled with undefined
values.
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 noticed this in the profiler while working on angular/material.angular.io#961. The `_applyBodyHighContrastModeCssClasses` method is called in the constructor of the `A11yModule` and `MatCommonModule` which means that it'll be invoked whenever a new root injector is created for a module importing any Material module (e.g. lazy-loaded route or dynamically-created component). Since it calls into `getHighContrastMode` and it touches the class list of `document.body`, it has the potential of causing some some slow layouts. This can be observed either when switching pages in the dev app or by profiling the example creation on material.angular.io. These changes resolve the issue by ensuring that we only run the logic once.
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.
LGTM, nice work!
@HostListener('mouseover') onMouseOver() { | ||
this.isDisabled = !this.stackBlitzForm; | ||
/** | ||
* Form used to submit the data to Stackblitz. |
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.
* Form used to submit the data to Stackblitz. | |
* Form used to submit the data to StackBlitz. |
* Important! it needs to be constructed ahead-of-time, because doing so on-demand | ||
* will cause Firefox to block the submit as a popup, because it didn't happen within | ||
* the same tick as the user interaction. |
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.
* Important! it needs to be constructed ahead-of-time, because doing so on-demand | |
* will cause Firefox to block the submit as a popup, because it didn't happen within | |
* the same tick as the user interaction. | |
* Important! It needs to be constructed ahead-of-time, because doing so on-demand | |
* would cause Firefox to block the submission as a popup. This is because it would't | |
* happen within the same tick as the user interaction. |
|
||
import {materialVersion} from '../version/version'; | ||
|
||
const STACKBLITZ_URL = 'https://run.stackblitz.com/api/angular/v1'; | ||
|
||
const COPYRIGHT = | ||
`Copyright 2020 Google LLC. All Rights Reserved. | ||
`Copyright ${new Date().getFullYear()} Google LLC. All Rights Reserved. |
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.
👏🏼
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.
lgtm
I noticed this in the profiler while working on angular/material.angular.io#961. The `_applyBodyHighContrastModeCssClasses` method is called in the constructor of the `A11yModule` and `MatCommonModule` which means that it'll be invoked whenever a new root injector is created for a module importing any Material module (e.g. lazy-loaded route or dynamically-created component). Since it calls into `getHighContrastMode` and it touches the class list of `document.body`, it has the potential of causing some some slow layouts. This can be observed either when switching pages in the dev app or by profiling the example creation on material.angular.io. These changes resolve the issue by ensuring that we only run the logic once.
I noticed this in the profiler while working on angular/material.angular.io#961. The `_applyBodyHighContrastModeCssClasses` method is called in the constructor of the `A11yModule` and `MatCommonModule` which means that it'll be invoked whenever a new root injector is created for a module importing any Material module (e.g. lazy-loaded route or dynamically-created component). Since it calls into `getHighContrastMode` and it touches the class list of `document.body`, it has the potential of causing some some slow layouts. This can be observed either when switching pages in the dev app or by profiling the example creation on material.angular.io. These changes resolve the issue by ensuring that we only run the logic once. (cherry picked from commit a92ad3d)
When generating an example in Stackblitz, we have to create a
form
element and submit it to a specific URL. Theform
has to be created eagerly, because some browsers will block us from submitting it if it is created asynchronously.As a result of this setup, we fire off a lot of HTTP requests when an example is rendered which slows the page down. These changes make the following improvements which shave off more than a second of scripting time when transitioning from the "Overview" to "Examples". I've used the datepicker examples as a benchmark.
I've also fixed that the copyright still said "2020".
For reference, here's what the flame chart looks like at the moment:

And this is after these changes:
