0
0
Supabasecloud~5 mins

Pagination patterns in Supabase - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Pagination patterns
O(n)
Understanding Time 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.

Scenario Under Consideration

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 Repeating Operations

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.
How Execution Grows With Input

Each page requires one API call, so more pages mean more calls.

Input Size (n pages)Approx. API Calls/Operations
1010 calls
100100 calls
10001000 calls

Pattern observation: The number of calls grows directly with the number of pages requested.

Final Time Complexity

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.

Common Mistake

[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.

Interview Connect

Understanding how pagination scales helps you design efficient data fetching in real projects.

Self-Check

"What if we changed from page-based to cursor-based pagination? How would the time complexity change?"