Skip to content
This repository was archived by the owner on Dec 18, 2024. It is now read-only.

perf: example rendering performance improvements #961

Merged
merged 1 commit into from
Apr 27, 2021

Conversation

crisbeto
Copy link
Member

@crisbeto crisbeto commented Apr 25, 2021

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. 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".

For reference, here's what the flame chart looks like at the moment:
Before

And this is after these changes:
After

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(() => {
Copy link
Member Author

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.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😬

crisbeto added a commit to crisbeto/material2 that referenced this pull request Apr 25, 2021
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.
@crisbeto crisbeto requested a review from devversion April 27, 2021 05:46
@crisbeto crisbeto merged commit b11ca05 into angular:master Apr 27, 2021
Copy link
Contributor

@Splaktar Splaktar left a 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* Form used to submit the data to Stackblitz.
* Form used to submit the data to StackBlitz.

Comment on lines +24 to +26
* 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏🏼

Copy link
Member

@jelbourn jelbourn left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

annieyw pushed a commit to angular/components that referenced this pull request May 3, 2021
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.
annieyw pushed a commit to angular/components that referenced this pull request May 3, 2021
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)
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants