Performance: Why file operations are common
MEDIUM IMPACT
File operations affect server response time and can delay sending data to the client, impacting page load speed and interaction responsiveness.
from fastapi import FastAPI, File, UploadFile import aiofiles app = FastAPI() @app.post('/upload') async def upload_file(file: UploadFile = File(...)): async with aiofiles.open('uploaded_file', 'wb') as out_file: while content := await file.read(1024): await out_file.write(content) return {'filename': file.filename}
from fastapi import FastAPI, File, UploadFile app = FastAPI() @app.post('/upload') async def upload_file(file: UploadFile = File(...)): contents = await file.read() with open('uploaded_file', 'wb') as f: f.write(contents) return {'filename': file.filename}
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Synchronous file read/write in FastAPI | N/A (server-side) | N/A | N/A | [X] Bad |
| Asynchronous chunked file read/write in FastAPI | N/A (server-side) | N/A | N/A | [OK] Good |