Skip to content

Conversation

@Goliworks
Copy link

@Goliworks Goliworks commented Nov 19, 2024

Clean some conditions and add missing types for more readability.

PR Checklist

Please check if your PR fulfills the following requirements:

PR Type

What kind of change does this PR introduce?

  • Bugfix
  • Feature
  • Code style update (formatting, local variables)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • CI related changes
  • Documentation content changes
  • angular.dev application / infrastructure changes
  • Other... Please describe:

What is the current behavior?

Issue Number: N/A

What is the new behavior?

Does this PR introduce a breaking change?

  • Yes
  • No

Other information

Clean some conditions and add missing types for more readability.
@pullapprove pullapprove bot requested review from alxhub and ellenyuan November 19, 2024 20:07
@pullapprove pullapprove bot added the requires: TGP This PR requires a passing TGP before merging is allowed label Nov 19, 2024
@angular-robot angular-robot bot added the area: core Issues related to the framework runtime label Nov 19, 2024
@ngbot ngbot bot added this to the Backlog milestone Nov 19, 2024
copy['type'] = EventType.POINTERLEAVE;

switch (e.type) {
case EventType.MOUSEOVER:
Copy link
Member

Choose a reason for hiding this comment

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

this is more verbose than the previous implementation and has a high bundle size impact.

Copy link
Author

Choose a reason for hiding this comment

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

Thanks for your feedback. I noticed that you didn't comment the first switch I implemented (at line n°22. in event.ts).
Was the change I made okay for you?

Copy link
Contributor

Choose a reason for hiding this comment

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

We'll actually need to physically build this to check for size regressions before we can merge this.

@thePunderWoman thePunderWoman requested review from iteriani, tbondwilkinson and thePunderWoman and removed request for alxhub and ellenyuan November 19, 2024 20:49
…n some conditions and add missing types for more readability.
@angular-robot angular-robot bot added area: core Issues related to the framework runtime and removed area: core Issues related to the framework runtime labels Nov 23, 2024
@Goliworks Goliworks requested a review from JeanMeche November 23, 2024 20:09
Comment on lines +22 to +32
switch (eventType) {
case EventType.MOUSEENTER:
return EventType.MOUSEOVER;
case EventType.MOUSELEAVE:
return EventType.MOUSEOUT;
case EventType.POINTERENTER:
return EventType.POINTEROVER;
case EventType.POINTERLEAVE:
return EventType.POINTEROUT;
default:
return eventType;
Copy link

Choose a reason for hiding this comment

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

Suggestion: instead of the if-else and the switch approaches why don't you go with a dictionary for object lookup? also it's to me better for maintenance (extension) and readability

Suggested change
switch (eventType) {
case EventType.MOUSEENTER:
return EventType.MOUSEOVER;
case EventType.MOUSELEAVE:
return EventType.MOUSEOUT;
case EventType.POINTERENTER:
return EventType.POINTEROVER;
case EventType.POINTERLEAVE:
return EventType.POINTEROUT;
default:
return eventType;
const eventMapping = {
[EventType.MOUSEENTER]: EventType.MOUSEOVER,
[EventType.MOUSELEAVE]: EventType.MOUSEOUT,
[EventType.POINTERENTER]: EventType.POINTEROVER,
[EventType.POINTERLEAVE]: EventType.POINTEROUT,
};
return eventMapping[eventType] || eventType;

Copy link
Author

Choose a reason for hiding this comment

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

I made some tests on jsperf.app and jsbench.me with these three solutions (if, switch and dictionary).
The dictionary had good perfs but only if you define the eventMapping outside the function. In this case it could be at the end of the file with the other const, otherwise it may be up to 90% slower than the previous approach. What do you think @JeanMeche?

Copy link
Member

Choose a reason for hiding this comment

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

Yes I wouldn't go with a map, as it introduces a lookup.

Copy link
Contributor

@wartab wartab Nov 26, 2024

Choose a reason for hiding this comment

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

Doing EventType.MOUSEENTER is also a lookup, since enums aren't actually a thing in JS and as far as I'm aware, enum values aren't inlined in the final bundle.

So the current code does actually do more lookups than than the object-map approach, since if you precompile the lookup-table as suggested, you are always doing one lookup, while the old code does always do at least two lookups and can do up to 5.

Copy link
Author

@Goliworks Goliworks Nov 26, 2024

Choose a reason for hiding this comment

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

Actually EventType isn't a typescript enum but a pure key value js object.
Is the lookup issue you're talking about still accurate in this case?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, it applies, it's basically what enum does under the hood. Sorry, I had checked EventType, which is also a type in the Angular router, where it actually is an enum and I didn't check if it's the correct one.

Copy link
Member

Choose a reason for hiding this comment

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

What can be costly in javascsript it accessing an unknown key. Please have a look at this explaination.

// `isContentEditable` is an old DOM API.
// tslint:disable-next-line:no-any
if ((el as any).isContentEditable) {
if (
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure we could consider this a readability improvement.
Both before and after would minified to the same code, so there is no runtime improvement.

Copy link
Author

@Goliworks Goliworks Nov 25, 2024

Choose a reason for hiding this comment

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

Interesting, I made this change to return false in a single condition to avoid repetition but if you say that the minified code would be the same I'm okay to put back the previous conditions everywhere I did it.

@thePunderWoman thePunderWoman marked this pull request as draft May 21, 2025 15:31
@thePunderWoman
Copy link
Contributor

If this were adding just types, this would be very safe to land, but since it has some changes to logic, there's some risk associated. This code is very size sensitive. So we'll close this for now. If you want to do something that just adds types, that should be quick to land. Thanks though!

@angular-automatic-lock-bot
Copy link

This issue has been automatically locked due to inactivity.
Please file a new issue if you are encountering a similar or related problem.

Read more about our automatic conversation locking policy.

This action has been performed automatically by a bot.

@angular-automatic-lock-bot angular-automatic-lock-bot bot locked and limited conversation to collaborators Jul 5, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area: core Issues related to the framework runtime requires: TGP This PR requires a passing TGP before merging is allowed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants