-
Notifications
You must be signed in to change notification settings - Fork 1
β‘ Bolt: Memoize and optimize conversation sorting #494
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-28 - Optimize React list sorting | ||
| **Learning:** For optimal performance in React list rendering, prefer lexicographical string comparison (e.g., `b.updatedAt.localeCompare(a.updatedAt)`) over `new Date()` allocation when sorting by ISO 8601 timestamps, and always memoize the derived list to prevent O(n log n) object allocation overhead on re-renders. | ||
| **Action:** Always memoize derived lists and avoid `new Date()` object allocation in sorting functions for ISO 8601 strings. | ||
| 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 lexicographical string comparison for O(n log n) object allocation overhead on re-renders | ||||||||||||||||||||||||||
| const sortedConversations = useMemo(() => { | ||||||||||||||||||||||||||
| return [...conversations].sort((a, b) => | ||||||||||||||||||||||||||
| b.updatedAt.localeCompare(a.updatedAt), | ||||||||||||||||||||||||||
| ); | ||||||||||||||||||||||||||
|
Comment on lines
+58
to
+61
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. Potentially incorrect sorting of conversations: return [...conversations].sort((a, b) => new Date(b.updatedAt).getTime() - new Date(a.updatedAt).getTime());This ensures conversations are sorted by actual date values, regardless of string format. |
||||||||||||||||||||||||||
| }, [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. While
Suggested change
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| 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.
It is better to recommend direct string comparison over
localeComparefor ISO 8601 timestamps to achieve better performance, especially since this is documented as an optimization learning.