Performance: Bulk operations
Bulk operations impact server response time and client rendering speed by reducing the number of requests and database transactions.
response = requests.post('/api/items/bulk', json=items)
process(response.json())for item in items: response = requests.post('/api/item', json=item) process(response.json())
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Multiple single requests | Multiple small updates | Multiple reflows | Higher paint cost due to delayed data | [X] Bad |
| Single bulk request | Single batch update | Single reflow | Lower paint cost with faster data | [OK] Good |
from fastapi import FastAPI
from pydantic import BaseModel
from typing import List
app = FastAPI()
class Item(BaseModel):
name: str
price: float
@app.post('/items/')
async def create_items(items: List[Item]):
return {'count': len(items), 'names': [item.name for item in items]}
What will be the response if the client sends this JSON body?
[{"name": "apple", "price": 1.2}, {"name": "banana", "price": 0.5}]from fastapi import FastAPI
from pydantic import BaseModel
app = FastAPI()
class User(BaseModel):
username: str
age: int
@app.post('/users/')
async def create_users(users: User):
return {'usernames': [user.username for user in users]}Pass the test and this pattern moves unknown