GET for reading resources in Rest API - Time & Space Complexity
When we use a GET request to read data from a server, it's important to understand how the time it takes grows as the amount of data grows.
We want to know: how does the work change when we ask for more or less data?
Analyze the time complexity of the following code snippet.
GET /items
// Server fetches all items from database
// Returns list of items as response
This code handles a GET request to read all items stored on the server and sends them back to the client.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The server reads each item from the database one by one.
- How many times: Once for each item stored (n times).
As the number of items grows, the server must read more data, so the work grows in a straight line with the number of items.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 reads |
| 100 | 100 reads |
| 1000 | 1000 reads |
Pattern observation: Doubling the items doubles the work needed.
Time Complexity: O(n)
This means the time to complete the GET request grows directly with the number of items requested.
[X] Wrong: "GET requests always take the same time no matter how much data is requested."
[OK] Correct: The server must read and send each item, so more items mean more work and more time.
Understanding how GET requests scale helps you explain how APIs handle data and what to expect when data grows, a useful skill in many programming tasks.
"What if the GET request only returned a fixed number of items regardless of total data size? How would the time complexity change?"