From 26d0265023963567c853c891c7d80cb36d9fa83f Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Sun, 23 Aug 2026 20:58:06 +0000 Subject: [PATCH] perf: optimize formatTime with precomputed array lookup Replaces dynamic padding and string allocation in formatTime with a pre-computed array lookup to improve render performance during high throughput messaging. --- .jules/bolt.md | 3 +++ bun.lock | 1 - src/cli/ui/components/messageList/utils.ts | 9 +++++---- 3 files changed, 8 insertions(+), 5 deletions(-) create mode 100644 .jules/bolt.md diff --git a/.jules/bolt.md b/.jules/bolt.md new file mode 100644 index 00000000..e6321f06 --- /dev/null +++ b/.jules/bolt.md @@ -0,0 +1,3 @@ +## 2026-08-23 - Optimizing time formatting in high-throughput render paths +**Learning:** In terminal UIs using React and Ink, formatting timestamps using `String().padStart()` inside the render loop causes significant overhead and unnecessary string allocations, leading to performance degradation in high-throughput message lists. +**Action:** Replace dynamic padding with a pre-computed string array lookup (`Array.from({length: 60})`) for bounded values like hours, minutes, and seconds (0-59). diff --git a/bun.lock b/bun.lock index f1109aa4..d19f7943 100644 --- a/bun.lock +++ b/bun.lock @@ -1,6 +1,5 @@ { "lockfileVersion": 1, - "configVersion": 1, "workspaces": { "": { "name": "salmon-loop", diff --git a/src/cli/ui/components/messageList/utils.ts b/src/cli/ui/components/messageList/utils.ts index ca9a3ed9..e59d523d 100644 --- a/src/cli/ui/components/messageList/utils.ts +++ b/src/cli/ui/components/messageList/utils.ts @@ -1,6 +1,7 @@ +// Pre-computed array lookup for time values (0-59) to avoid string allocations +// and padStart() overhead during high-throughput React rendering +const timeLookup = Array.from({ length: 60 }, (_, i) => String(i).padStart(2, '0')); + export function formatTime(timestamp: Date): string { - const hours = String(timestamp.getHours()).padStart(2, '0'); - const minutes = String(timestamp.getMinutes()).padStart(2, '0'); - const seconds = String(timestamp.getSeconds()).padStart(2, '0'); - return `${hours}:${minutes}:${seconds}`; + return `${timeLookup[timestamp.getHours()] as string}:${timeLookup[timestamp.getMinutes()] as string}:${timeLookup[timestamp.getSeconds()] as string}`; }