Skip to content

⚡ Optimize User Store with Caching and useSyncExternalStore#32

Open
APPLEPIE6969 wants to merge 3 commits intomainfrom
perf-optimize-user-store-14157725495662008386
Open

⚡ Optimize User Store with Caching and useSyncExternalStore#32
APPLEPIE6969 wants to merge 3 commits intomainfrom
perf-optimize-user-store-14157725495662008386

Conversation

@APPLEPIE6969
Copy link
Copy Markdown
Owner

This PR addresses a performance issue where the user profile was being accessed synchronously from localStorage during the render cycle of several key components.

💡 What:

  • Introduced a caching layer and a subscription-based listener system in lib/userStore.ts.
  • Implemented the useUserProfile hook using React's useSyncExternalStore API.
  • Refactored components/Sidebar.tsx, app/dashboard/page.tsx, and app/profile/page.tsx to use this reactive hook.
  • Switched to immutable data updates in lib/userStore.ts to ensure referential stability.

🎯 Why:

Synchronous access to localStorage during render blocks the main thread and causes hydration mismatches in Next.js because localStorage is unavailable on the server. By moving this to a reactive hook with a cache, we improve both performance and reliability.

📊 Measured Improvement:

While environment constraints prevented running a full automated benchmark suite, the architectural shift from O(N) redundant synchronous I/O and parsing to O(1) cached memory access per render is a significant theoretical improvement. It also eliminates React hydration warnings related to user profile data.


PR created automatically by Jules for task 14157725495662008386 started by @APPLEPIE6969

- Implemented an in-memory cache in `lib/userStore.ts` to avoid redundant `localStorage` access and `JSON.parse` operations.
- Added a reactive `useUserProfile` hook using `useSyncExternalStore` for efficient, hydration-safe access to user data.
- Refactored `Sidebar`, `Dashboard`, and `Profile` components to use the new hook, resolving synchronous I/O in the render path and potential hydration mismatches.
- Updated profile modification functions to use immutable update patterns and notify subscribers.
- Cleaned up unused imports.

Co-authored-by: APPLEPIE6969 <242827480+APPLEPIE6969@users.noreply.github.com>
@google-labs-jules
Copy link
Copy Markdown
Contributor

👋 Jules, reporting for duty! I'm here to lend a hand with this pull request.

When you start a review, I'll add a 👀 emoji to each comment to let you know I've read it. I'll focus on feedback directed at me and will do my best to stay out of conversations between you and other bots or reviewers to keep the noise down.

I'll push a commit with your requested changes shortly after. Please note there might be a delay between these steps, but rest assured I'm on the job!

For more direct control, you can switch me to Reactive Mode. When this mode is on, I will only act on comments where you specifically mention me with @jules. You can find this option in the Pull Request section of your global Jules UI settings. You can always switch back!

New to Jules? Learn more at jules.google/docs.


For security, I will only act on instructions from the user who triggered this task.

@vercel
Copy link
Copy Markdown
Contributor

vercel bot commented Feb 26, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
studyflow Ready Ready Preview, Comment Feb 26, 2026 3:20pm

- Implemented an in-memory cache in `lib/userStore.ts` to avoid redundant `localStorage` access and `JSON.parse` operations.
- Added a reactive `useUserProfile` hook using `useSyncExternalStore` for efficient, hydration-safe access to user data.
- Refactored `Sidebar`, `Dashboard`, and `Profile` components to use the new hook, resolving synchronous I/O in the render path and potential hydration mismatches.
- Updated profile modification functions to use immutable update patterns and notify subscribers.
- Cleaned up unused imports.

Co-authored-by: APPLEPIE6969 <242827480+APPLEPIE6969@users.noreply.github.com>
- Implemented an in-memory cache in `lib/userStore.ts` to avoid redundant `localStorage` access and `JSON.parse` operations.
- Added a reactive `useUserProfile` hook using `useSyncExternalStore` for efficient, hydration-safe access to user data.
- Refactored `Sidebar`, `Dashboard`, and `Profile` components to use the new hook, resolving synchronous I/O in the render path and potential hydration mismatches.
- Updated profile modification functions to use immutable update patterns and notify subscribers.
- Cleaned up unused imports.

Co-authored-by: APPLEPIE6969 <242827480+APPLEPIE6969@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant