翻篇收录说明
翻篇网页快照

Memoization

https://streamdown.ai/docs/memoization

最近抓取:2026-12-04 02:59:42(北京时间)

最近内容变化:2026-12-01 00:15:15(北京时间)

这是最近一次有效抓取的文本快照,不含网页脚本、图片和动态交互内容。实际网页可能已更新。

访问原网页

页面简介

Performance optimization through intelligent memoization and caching.

正文摘录

MenuMemoizationCopy for LLMView MarkdownAsk AI about this pageStreamdown is built with performance in mind, utilizing React's memoization capabilities to ensure efficient rendering even with large amounts of streaming content. The library intelligently caches computations and prevents unnecessary re-renders, making it ideal for real-time AI streaming applications. How Memoization Works Streamdown implements memoization at multiple levels to maximize performance: Component-Level Memoization The main Streamdown component is wrapped with React.memo, which prevents re-renders when props haven't changed: import { Streamdown } from 'streamdown'; export default function Page() { const markdown = "# Hello World\n\nThis is **streaming** markdown!"; return <Streamdown>{markdown}</Streamdown>; } The component only re-renders when one of these props changes: children (markdown content) shikiTheme isAnimating animated mode plugins className linkSafety normalizeHtmlIndentation All other prop changes are ignored, ensuring optimal performance. Block-Level Memoization Streamdown parses markdown content into individual blocks, with each block memoized separately. This means: Only blocks with changed content are re-rendered Unchanged blocks remain memoized, even if new blocks are added Parsing is cached per block for efficiency Expensive computations are also cached using useMemo. Performance Benefits Streaming Efficiency When content is streaming in, Streamdown's memoization strategy ensures: Incremental Rendering - Only new or changed blocks are processed Stable Output - Completed blocks remain stable and don't re-render Minimal Overhead - Parsing and rendering work is minimized Example with streaming content: 'use client'; import { useChat } from '@ai-sdk/react'; import { Streamdown } from 'streamdown'; export default function Chat() { const { messages, sendMessage, status } = useChat(); return ( <> {messages.map(message => ( <div key={message.id}> {message.parts.filter(part => part.type === 'text').map((part, index) => ( <Streamdown isAnimating={status === 'streaming'} key={index} > {part.text} </Streamdown> ))} </div> ))} </> ); } In this example: As new tokens arrive, only the affected blocks are re-rendered Previous blocks remain memoized and stable The rendering performance stays consistent regardless of content length Syntax Highlighting Cache The syntax highlighter maintains an internal cache of loaded languages and themes. This means: Languages are loaded once and cached for reuse Theme changes don't require reloading languages Multiple code blocks in the same language share the highlighter instance PreviousLink SafetyNextSecurity