From 576ee9243890e612652886d46c464e86b4f1a6f4 Mon Sep 17 00:00:00 2001 From: Classic298 <27028174+Classic298@users.noreply.github.com> Date: Fri, 6 Mar 2026 22:36:13 +0100 Subject: [PATCH] perf: rewrite createMessagesList from recursive to iterative (#22194) Replace the recursive spread-based implementation with an iterative push+reverse approach. The recursive version created a new array at each level of recursion via spread, resulting in O(d^2) array copies where d is the conversation depth. The iterative version walks from the target message to the root, pushes each message, and reverses once at the end for O(d) total work. No behavioral change - same input produces the same output array. --- src/lib/utils/index.ts | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/src/lib/utils/index.ts b/src/lib/utils/index.ts index 4a1cf2e91..18c5c6b67 100644 --- a/src/lib/utils/index.ts +++ b/src/lib/utils/index.ts @@ -1181,19 +1181,19 @@ export const getWeekday = () => { }; export const createMessagesList = (history, messageId) => { - if (messageId === null) { - return []; + const list = []; + let currentId = messageId; + + while (currentId !== null && currentId !== undefined) { + const message = history.messages[currentId]; + if (message === undefined) { + break; + } + list.push(message); + currentId = message.parentId; } - const message = history.messages[messageId]; - if (message === undefined) { - return []; - } - if (message?.parentId) { - return [...createMessagesList(history, message.parentId), message]; - } else { - return [message]; - } + return list.reverse(); }; export const formatFileSize = (size) => {