perf: replace O(n²) unshift with O(n) push+reverse in buildMessages (#22280)

Array.unshift() is O(n) per call because it shifts all existing
elements. In a loop building an n-element array, this makes the
total cost O(n²). Replace with push() + reverse() which is O(n)
total. Produces the identical message ordering.
This commit is contained in:
Classic298
2026-03-06 21:02:57 +01:00
committed by GitHub
parent 339ed1d72e
commit 1850a985b5

View File

@@ -90,11 +90,11 @@
}
visitedMessageIds.add(message.id);
_messages.unshift(message);
_messages.push(message);
message = message.parentId !== null ? history.messages[message.parentId] : null;
}
messages = _messages;
messages = _messages.reverse();
};
// Throttle message list rebuilds to once per animation frame during streaming.