Performance: Including routers in main app
MEDIUM IMPACT
This affects the initial server startup time and the routing lookup speed during request handling.
from fastapi import FastAPI, APIRouter items_router = APIRouter() @items_router.get('/items') def read_items(): return {'items': []} users_router = APIRouter() @users_router.get('/users') def read_users(): return {'users': []} app = FastAPI() app.include_router(items_router) app.include_router(users_router)
from fastapi import FastAPI app = FastAPI() @app.get('/items') def read_items(): return {'items': []} @app.get('/users') def read_users(): return {'users': []}
| Pattern | Routing Table Size | Startup Time | Request Routing Speed | Verdict |
|---|---|---|---|---|
| All routes on main app | Large (all routes in one table) | Longer (registers all routes at once) | Slower (linear search through routes) | [X] Bad |
| Routes organized in routers | Smaller per router, combined efficiently | Shorter (modular registration) | Faster (optimized routing lookup) | [OK] Good |