-
Notifications
You must be signed in to change notification settings - Fork 1
β‘ Bolt: Optimize list sorting performance in ConversationsSidebar #501
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ## 2026-04-30 - Optimize React List Rendering Timestamp Sorting | ||
| **Learning:** Using new Date() allocations inside array sorts within React components leads to unnecessary memory allocation and performance overhead on every re-render. | ||
| **Action:** Always memoize derived lists that require sorting and prefer raw string comparisons (e.g., b.updatedAt > a.updatedAt) for ISO 8601 timestamp fields over allocating Date objects. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,7 +2,7 @@ | |
| * Conversations sidebar component β left sidebar with conversation list. | ||
| */ | ||
|
|
||
| import { useEffect, useRef, useState } from "react"; | ||
| import { useEffect, useMemo, useRef, useState } from "react"; | ||
| import { useApp } from "../AppContext"; | ||
|
|
||
| interface ConversationsSidebarProps { | ||
|
|
@@ -54,11 +54,12 @@ export function ConversationsSidebar({ | |
| } | ||
| }, [editingId]); | ||
|
|
||
| const sortedConversations = [...conversations].sort((a, b) => { | ||
| const aTime = new Date(a.updatedAt).getTime(); | ||
| const bTime = new Date(b.updatedAt).getTime(); | ||
| return bTime - aTime; | ||
| }); | ||
| const sortedConversations = useMemo(() => { | ||
| return [...conversations].sort((a, b) => { | ||
| // β‘ Bolt: Memoize the sorting and use raw string comparison for O(n log n) performance without Date allocation overhead | ||
| return b.updatedAt > a.updatedAt ? 1 : b.updatedAt < a.updatedAt ? -1 : 0; | ||
| }); | ||
| }, [conversations]); | ||
|
Comment on lines
+57
to
+62
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Potential Incorrect Sorting of Conversations The sorting logic in useMemo compares return b.updatedAt > a.updatedAt ? 1 : b.updatedAt < a.updatedAt ? -1 : 0;This approach assumes that Recommended Solution: return Date.parse(b.updatedAt) - Date.parse(a.updatedAt);This guarantees correct chronological sorting regardless of the string format. |
||
|
|
||
| const handleDoubleClick = (conv: { id: string; title: string }) => { | ||
| setEditingId(conv.id); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This file appears to be a tool-generated learning log. While the optimization logic is sound, including such metadata files in the source repository can lead to clutter and is generally not considered a best practice for repository cleanliness. Consider moving these logs to a separate documentation system, a wiki, or a dedicated metadata store outside of the application source tree.
Additionally, the action described here should be applied consistently across the codebase. For instance,
apps/app/src/AppContext.tsxstill contains multiple instances ofnew Date().getTime()sorting (e.g., lines 5034 and 5051) that would benefit from this optimization.