Performance: Response model declaration
MEDIUM IMPACT
This affects the API response serialization speed and the size of data sent over the network, impacting time to first byte and overall response time.
from fastapi import FastAPI from pydantic import BaseModel class ItemResponse(BaseModel): id: int name: str app = FastAPI() @app.get("/items/{item_id}", response_model=ItemResponse) async def read_item(item_id: int): item = get_item_from_db(item_id) return item
from fastapi import FastAPI app = FastAPI() @app.get("/items/{item_id}") async def read_item(item_id: int): item = get_item_from_db(item_id) # returns full DB model return item
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| No response model, full DB object returned | N/A | N/A | Larger JSON payload delays browser rendering | [X] Bad |
| Response model declared with limited fields | N/A | N/A | Smaller JSON payload speeds up browser rendering | [OK] Good |