Performance: Pagination patterns
Pagination patterns affect how quickly data loads and renders on the page, impacting load speed and interaction responsiveness.
from fastapi import FastAPI, Query app = FastAPI() @app.get('/items') async def read_items(page: int = Query(1, ge=1), size: int = Query(10, ge=1, le=100)): # Returns only a page of items offset = (page - 1) * size return get_items_from_db(offset=offset, limit=size)
from fastapi import FastAPI app = FastAPI() @app.get('/items') async def read_items(): # Returns all items at once return get_all_items_from_db()
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Load all data at once | High (all items in DOM) | Many reflows | High paint cost | [X] Bad |
| Server-side pagination | Low (page items only) | Single reflow | Low paint cost | [OK] Good |
| Client-side pagination with all data | High (all items in DOM) | Many reflows | High paint cost | [X] Bad |
| Client-side pagination with page data | Low (page items only) | Single reflow | Low paint cost | [OK] Good |
?skip=2&limit=3 on a list [10, 20, 30, 40, 50, 60]?
items = [10, 20, 30, 40, 50, 60]
@app.get('/items')
def read_items(skip: int = 0, limit: int = 5):
return items[skip : skip + limit]@app.get('/users')
def get_users(page: int = 1, size: int = 10):
skip = page * size
users = get_all_users()
return users[skip : skip + size]Pass the test and this pattern moves unknown