Skip to content

Commit 114b6e8

Browse files
committed
perf: reconcile markdown tokens to skip unchanged blocks
1 parent 473d971 commit 114b6e8

1 file changed

Lines changed: 13 additions & 1 deletion

File tree

src/lib/components/chat/Messages/Markdown.svelte

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,18 @@
6262
let pendingUpdate = null;
6363
let lastContent = '';
6464
let lastParsedContent = '';
65+
let lastTokens = [];
66+
67+
// Keep the same object for unchanged blocks so the keyed each skips re-rendering them
68+
const reconcileTokens = (next) => {
69+
for (let i = 0; i < next.length; i++) {
70+
if (lastTokens[i] && lastTokens[i].raw === next[i].raw) {
71+
next[i] = lastTokens[i];
72+
}
73+
}
74+
lastTokens = next;
75+
return next;
76+
};
6577
6678
const parseTokens = () => {
6779
if (content === lastContent) return;
@@ -71,7 +83,7 @@
7183
if (processed === lastParsedContent) return;
7284
lastParsedContent = processed;
7385
74-
tokens = marked.lexer(processed);
86+
tokens = reconcileTokens(marked.lexer(processed));
7587
};
7688
7789
const updateHandler = (content) => {

0 commit comments

Comments
 (0)