-
Notifications
You must be signed in to change notification settings - Fork 1
β‘ Bolt: Optimize conversation list sorting #491
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 @@ | ||
| ## 2025-04-27 - Fast Lexicographical Time Sorting | ||
| **Learning:** In heavily used UI components like React sidebars that display lists updated in real-time, relying on `.sort()` blocks combined with inline `new Date(string).getTime()` allocation causes measurable re-render lag due to memory allocation and date parsing overhead on every tick. For standard ISO 8601 strings (like `updatedAt`), the sorting structure is inherently chronologically sound and allows for fast alphabetical comparison. | ||
| **Action:** When implementing real-time list sorting by ISO timestamp on the frontend, always combine array memoization (`useMemo`) with native `.localeCompare()` instead of allocating `Date` objects on every re-render. |
| 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; | ||
| }); | ||
| // β‘ Bolt: Memoize the sorting and use localeCompare for timestamps instead of parsing Date | ||
| const sortedConversations = useMemo(() => { | ||
| return [...conversations].sort((a, b) => | ||
| b.updatedAt.localeCompare(a.updatedAt), | ||
| ); | ||
| }, [conversations]); | ||
|
|
||
|
Comment on lines
+57
to
63
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. Using [...conversations].sort((a, b) => new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime())This approach is more resilient to format changes and ensures correct chronological ordering. |
||
| 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.
Using direct string comparison (
>) for ISO 8601 timestamps is significantly more efficient thanlocaleCompare. Since ISO strings are designed to be lexicographically sortable, the locale-aware collation logic inlocaleCompareis unnecessary and adds avoidable overhead in this performance-critical path.