-
Notifications
You must be signed in to change notification settings - Fork 1
β‘ Bolt: [performance improvement] Memoize and optimize conversation sorting #496
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 @@ | ||
| ## 2024-04-28 - Component Array Sorting Performance | ||
| **Learning:** In React components like `ConversationsSidebar`, copying and sorting arrays on every render (e.g., `[...conversations].sort(...)`) causes unnecessary object allocation and CPU overhead, especially if the component has local state causing frequent re-renders (like input selection). Additionally, when dealing with standard ISO 8601 timestamp strings, allocating `new Date()` objects purely for chronological sorting is an expensive anti-pattern. | ||
| **Action:** Always wrap array mapping/sorting in `useMemo` when deriving lists from props. Furthermore, use lexicographical string comparison (`b.localeCompare(a)`) to sort ISO 8601 date strings instead of parsing them into `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,11 @@ 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(() => { | ||||||
| // β‘ Bolt: Memoize the sorting and use lexicographical string comparison | ||||||
| // for ISO 8601 timestamps to avoid expensive new Date() allocations on every render. | ||||||
| return [...conversations].sort((a, b) => 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]); | ||||||
|
|
||||||
| 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 to use
localeComparefor ISO 8601 strings is slightly suboptimal for performance-critical code. Direct string comparison is faster and sufficient for these timestamps.