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.
This commit is contained in:
+11
-11
@@ -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) => {
|
||||
|
||||
Reference in New Issue
Block a user