Skip to content
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

♻️ Reduce size of interactive extension #30233

Merged
merged 7 commits into from
Sep 16, 2020

Conversation

mszylkowski
Copy link
Contributor

@mszylkowski mszylkowski commented Sep 15, 2020

Managed to reduce size of interactive components significantly:

69.270kb -> Before
65.420kb -> After removing the unnecessary import of all LocalizedIds, by refactoring the uses.
61.778kb -> After simplifying quiz CSS, check https://codepen.io/mszylkowski/pen/BaKPppN to see new animation

These changes decrease the size of the extension by 11%

Closes #29282

@amp-owners-bot
Copy link

Hey @gmajoulet! These files were changed:

extensions/amp-story-interactive/0.1/amp-story-interactive-quiz.css
extensions/amp-story-interactive/0.1/amp-story-interactive-quiz.js

@mszylkowski mszylkowski marked this pull request as draft September 15, 2020 17:04
@gmajoulet
Copy link
Contributor

So no updates to the store service are needed? The service class itself isn't duplicated across extension builds?

@mszylkowski
Copy link
Contributor Author

mszylkowski commented Sep 15, 2020

The problem is how the array is being used. From the compiler standpoint, if you access a const object with the square brackets, it will need to import the whole object as the keys are calculated in runtime. If you access the keys with the dot notation, the compiler optimizes that and the compiled code ends up being answers = ["76", "77", "78", "79"], removing the step of fetching the value per key.

The services class is only used with the static functions so the functions are compiled and substituted in place. The Services class doesn't get bundled.

Only weird thing I'm investigating is that it's including the experiments.js file...

@mszylkowski mszylkowski marked this pull request as ready for review September 15, 2020 19:44
@mszylkowski
Copy link
Contributor Author

Update: experiments are part of core amp and are embedded into all the extensions. I feel like this should not happen, but it would require a lot of digging to remove it safely

/** @private {!Array<string>} */
this.answerChoiceOptions_ = ['A', 'B', 'C', 'D'];
/** @private {!Array<../../../src/localized-strings.LocalizedStringId>} */
this.answerChoiceOptions_ = [
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: let's update this variable name to reflect what's inside it. I know you mutate it later, which makes it even more confusing because the type is no longer correct

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's true, technically the type is always a string but pr-check required it to be this type in the JSDoc. I'll change the name so that it shows it can be the localized values

Copy link
Contributor

Choose a reason for hiding this comment

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

If having two variables (one for the localizedIds and one for the strings) make things easier to understand and more accurate, we should go for it

Copy link
Contributor

Choose a reason for hiding this comment

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

Alternately, we could construct the localizedIds array inline, right before sending them through the localization service.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't feel the need to have two variables, since they just clutter the component (many components have so many variables that it's hard to understand what they do). Because of the type inconsistency, constructing the array in-line makes the most sense, as the LocalizedStringId keys won't be used anywhere else. Sent the commit with it

@mszylkowski mszylkowski merged commit 18ef676 into ampproject:master Sep 16, 2020
@mszylkowski mszylkowski deleted the interactive_strip_size branch September 16, 2020 21:42
ed-bird pushed a commit to ed-bird/amphtml that referenced this pull request Dec 10, 2020
* Reduced quizzes size by 8.5kb

* Reformatted code

* Removed unused dev

* Fixed type conversion

* Renamed answerChoices

* Moved localizedStringIds to inline
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Make sure the story store service is not included in sub extensions bundles.
3 participants