Skip to content

⚡ Bolt: Memoize LiquidStream messages to prevent O(N^2) renders#80

Open
teerthsharma wants to merge 1 commit into
mainfrom
bolt-liquidstream-memoization-15229860094832759570
Open

⚡ Bolt: Memoize LiquidStream messages to prevent O(N^2) renders#80
teerthsharma wants to merge 1 commit into
mainfrom
bolt-liquidstream-memoization-15229860094832759570

Conversation

@teerthsharma

Copy link
Copy Markdown
Owner

💡 What:
Extracted the inline message rendering logic in LiquidStream into a new MessageItem component and wrapped it in React.memo(). Initialized the timestamp via a useState hook with a lazy initializer to calculate it only once on mount.

🎯 Why:
The messages array in LiquidStream updates frequently (simulating incoming data streams). Rendering the list items directly inside the .map() block meant that every new message appended to the array triggered a full re-render of all existing messages in the stream. This creates an $O(N^2)$ rendering bottleneck that makes the component progressively slower as the chat history grows. Furthermore, new Date().toLocaleTimeString() was being called inline on every render, causing all previously recorded timestamps in the history to incorrectly update to the current time whenever a new message arrived or the user typed in the input box.

📊 Impact:

  • Eliminates $O(N^2)$ full-list re-renders when the message stream updates. Existing message components bypass the reconciliation phase entirely.
  • Fixes a timestamp recalculation bug; historical messages now retain their correct arrival times.
  • Significantly improves responsiveness of the user input field.

🔬 Measurement:

  • Start the Next.js server locally, navigate to a page rendering the LiquidStream component, and observe the React Profiler. You will see that previously rendered MessageItem components do not re-render when new messages are added to the list.

PR created automatically by Jules for task 15229860094832759570 started by @teerthsharma

Co-authored-by: teerthsharma <78080953+teerthsharma@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.

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