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

[Feature] Improve breakpoint performance #589

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Improve breakpoint retrieval performance
The `matchMedia` function in Safari is quite slow. If we try to access
the breakpoint inside an `requestAnimationFrame` loop, it gets slow.
titouanmathis committed Jan 27, 2025
commit a35ee52e730b90b6ef8633d586995a092fd315f2
6 changes: 5 additions & 1 deletion packages/js-toolkit/services/ResizeService.ts
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ import { AbstractService } from './AbstractService.js';
import type { Features } from '../Base/features.js';
import { features } from '../Base/features.js';
import { debounce } from '../utils/debounce.js';
import { cache } from '../utils/cache.js';

export interface ResizeServiceProps<U extends Features['breakpoints'] = Features['breakpoints']> {
width: number;
@@ -51,7 +52,10 @@ export class ResizeService<
for (const [name, breakpoint] of Object.entries(
this.breakpoints ?? features.get('breakpoints'),
)) {
activeBreakpoints[name] = window.matchMedia(`(min-width: ${breakpoint})`).matches;
activeBreakpoints[name] = cache(
[this, window.innerWidth, window.innerHeight, breakpoint],
() => window.matchMedia(`(min-width: ${breakpoint})`).matches,
);
}

return activeBreakpoints as Record<keyof T, boolean>;