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

[WOM-5333] Wrap BpkCalendar format date methods with memoize #3689

Merged
merged 6 commits into from
Dec 5, 2024
Merged

Conversation

sarak-works
Copy link
Contributor

Since INP has been introduced as a Core Web Vital (CWV) in March 2024, Skyscanner has struggled to maintain a good rating.

INP stands for “Interaction To Next Paint” and is essentially the time it takes for a UI to respond to a users action (like a press or a click). A lot of evidence suggests that a lot of this is down to the Calendar found in our design system “Backpack”.

Analysis shows that we spend a lot of CPU/compute on formatting dates, which we do multiple times on each render for each date. This compute causes the UI to be slow and unresponsive on CPU limited devices (like your great aunts old Samsung Android Phone).

The theory is that using a Memoize function in front of these expensive functions can bring down this compute cost.

@sarak-works sarak-works added the minor Non breaking change label Dec 4, 2024
Copy link

github-actions bot commented Dec 4, 2024

Visit https://backpack.github.io/storybook-prs/3689 to see this build running in a browser.

Copy link

github-actions bot commented Dec 4, 2024

Warnings
⚠️

Package source files (e.g. packages/package-name/src/Component.js) were updated, but snapshots weren't. Have you checked that the tests still pass?

⚠️

Package source files (e.g. packages/package-name/src/Component.tsx) were updated, but type files weren't. Have you checked that no types have changed?

Browser support

If this is a visual change, make sure you've tested it in multiple browsers.

Generated by 🚫 dangerJS against 0bd6fe4

@metalix2
Copy link
Contributor

metalix2 commented Dec 4, 2024

Hi @sarak-works, Thanks for the contribution! I suppose we could validate this be memoizing the props that are passed into the composeCalendar function? Then it could be proven before making this change? Though I don't think this is a blocker for this PR.

@@ -19,6 +19,8 @@
import type { ElementType } from 'react';
import { Component } from 'react';

import { memoize } from 'lodash';
Copy link
Contributor

Choose a reason for hiding this comment

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

memoize is decent sized package. Could we memoize a different way to avoid bundling that? Like using a map instead
https://bundlephobia.com/package/lodash.memoize@4.1.2

formatDateFull={formatDateFull}
formatMonth={formatMonth}
formatDateFull={memoize(formatDateFull)}
formatMonth={memoize(formatMonth)}
Copy link
Contributor

Choose a reason for hiding this comment

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

I looked into this before and I think there is a case where you might be memoizing twice here if the consumer uses BpkCalendarGrid as the "Grid"....which I think most consumers will

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 see, so I guess it'd be safe to only memoize as part of BpkCalendarGrid and monitor it

@igunsaya
Copy link
Contributor

igunsaya commented Dec 4, 2024

Hi @sarak-works, Thanks for the contribution! I suppose we could validate this be memoizing the props that are passed into the composeCalendar function? Then it could be proven before making this change? Though I don't think this is a blocker for this PR.

We made a similar fix in the GC and found it to have the desired effect of reducing the time spent in calling formatDate and hence date-fns 👿

Copy link

github-actions bot commented Dec 5, 2024

Visit https://backpack.github.io/storybook-prs/3689 to see this build running in a browser.

Copy link

github-actions bot commented Dec 5, 2024

Visit https://backpack.github.io/storybook-prs/3689 to see this build running in a browser.

Copy link

github-actions bot commented Dec 5, 2024

Visit https://backpack.github.io/storybook-prs/3689 to see this build running in a browser.

@sarak-works sarak-works requested review from igunsaya and Sybsw December 5, 2024 07:48
Copy link

github-actions bot commented Dec 5, 2024

Visit https://backpack.github.io/storybook-prs/3689 to see this build running in a browser.

@Sybsw Sybsw merged commit a3d4e5d into main Dec 5, 2024
9 checks passed
@Sybsw Sybsw deleted the WOM-5333 branch December 5, 2024 09:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
minor Non breaking change
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants