Pagination patterns in Supabase - Time & Space Complexity
When fetching data in pages, the time it takes depends on how many pages and items we request.
We want to know how the number of data requests grows as we ask for more pages.
Analyze the time complexity of fetching paged data using Supabase.
const pageSize = 10;
let page = 1;
const { data, error } = await supabase
.from('items')
.select('*')
.range((page - 1) * pageSize, page * pageSize - 1);
// This fetches one page of items with a fixed size.
This code fetches one page of data with a fixed number of items per page.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: One API call to fetch a page of data.
- How many times: Once per page requested.
Each page requires one API call, so more pages mean more calls.
| Input Size (n pages) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 calls |
| 100 | 100 calls |
| 1000 | 1000 calls |
Pattern observation: The number of calls grows directly with the number of pages requested.
Time Complexity: O(n)
This means if you want to get more pages, the total work grows in a straight line with the number of pages.
[X] Wrong: "Fetching more pages is just one big call, so time stays the same."
[OK] Correct: Each page requires a separate call, so more pages mean more calls and more time.
Understanding how pagination scales helps you design efficient data fetching in real projects.
"What if we changed from page-based to cursor-based pagination? How would the time complexity change?"