Why hypermedia drives discoverability in Rest API - Performance Analysis
When working with hypermedia in REST APIs, it's important to understand how the number of operations grows as clients discover new links dynamically.
We want to see how the process of following links affects the total work done by the client.
Analyze the time complexity of this simplified hypermedia client code.
function fetchResources(url) {
fetch(url).then(response => response.json()).then(data => {
data.links.forEach(link => {
fetchResources(link.href);
});
});
}
fetchResources('https://api.example.com/root');
This code fetches a resource, then recursively fetches all linked resources it finds.
Look at what repeats as the client discovers new links.
- Primary operation: Fetching each resource and iterating over its links.
- How many times: Once per resource discovered, potentially many times as links lead to more resources.
As the number of linked resources grows, the client fetches more and more resources.
| Input Size (number of resources) | Approx. Operations (fetches) |
|---|---|
| 10 | About 10 fetch calls |
| 100 | About 100 fetch calls |
| 1000 | About 1000 fetch calls |
Pattern observation: The number of fetches grows roughly in direct proportion to the number of resources linked.
Time Complexity: O(n)
This means the total work grows linearly with the number of resources discovered through links.
[X] Wrong: "Following links in hypermedia is free or constant time because each fetch is independent."
[OK] Correct: Each link leads to a new fetch, so the total work adds up as more resources are discovered.
Understanding how hypermedia-driven discovery scales helps you explain API client behavior clearly and shows you grasp how dynamic data affects performance.
What if we limited the number of links followed per resource? How would that change the time complexity?