-
Notifications
You must be signed in to change notification settings - Fork 1
β‘ Bolt: Optimize ConversationsSidebar sorting #489
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: main
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-26 - React List Sorting Bottlenecks | ||
| **Learning:** In React components that render lists (like `ConversationsSidebar.tsx`), sorting an array inside the render cycle without `useMemo` causes an O(n log n) operation to run on *every single render*. Furthermore, allocating `new Date()` instances for comparison inside the sort callback adds significant heap allocation overhead, compounding the performance issue. | ||
| **Action:** When sorting arrays for rendering, wrap the sorted derived state in `useMemo`. When comparing ISO 8601 strings (like `updatedAt`), use string comparison (`b.updatedAt.localeCompare(a.updatedAt)`) to prevent unnecessary `Date` allocations. | ||
| 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 of conversations and use string comparison for ISO 8601 dates to avoid O(N log N) Date allocations on every render | ||||||
| const sortedConversations = useMemo(() => { | ||||||
| return [...conversations].sort((a, b) => { | ||||||
| return b.updatedAt.localeCompare(a.updatedAt); | ||||||
|
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
|
||||||
| }); | ||||||
| }, [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. The sorting logic in return [...conversations].sort((a, b) => {
if (!a.updatedAt || !b.updatedAt) return 0;
return b.updatedAt.localeCompare(a.updatedAt);
});Alternatively, log or handle cases where |
||||||
| 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.
The recommendation should favor standard string comparison operators over
localeComparefor ISO 8601 strings.localeCompareis much slower and unnecessary for strings that are already lexicographically sortable.