Performance: Streaming responses
MEDIUM IMPACT
Streaming responses affect how quickly the user starts receiving data and how smoothly the content loads over time.
from fastapi import FastAPI from fastapi.responses import StreamingResponse import asyncio app = FastAPI() async def data_generator(): for i in range(10): yield f'data chunk {i}\n' await asyncio.sleep(0.1) @app.get('/stream') async def stream_data(): return StreamingResponse(data_generator(), media_type='text/plain')
from fastapi import FastAPI app = FastAPI() @app.get('/data') async def get_data(): result = compute_large_data() return result
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Full response after processing | Single DOM update after full data | Single reflow after full load | One large paint | [X] Bad |
| Streaming response with chunks | Incremental DOM updates as chunks arrive | Multiple small reflows | Multiple small paints | [OK] Good |