Performance testing in GraphQL - Time & Space Complexity
Performance testing helps us see how fast a GraphQL query runs as data grows.
We want to know how the work done changes when we ask for more data.
Analyze the time complexity of the following code snippet.
query GetUsersWithPosts($limit: Int) {
users(limit: $limit) {
id
name
posts {
id
title
}
}
}
This query fetches a list of users with their posts, limited by a number.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Fetching each user and then fetching all their posts.
- How many times: Once per user, and once per post for each user.
Explain the growth pattern intuitively.
| Input Size (n users) | Approx. Operations |
|---|---|
| 10 | About 10 users plus their posts |
| 100 | About 100 users plus their posts |
| 1000 | About 1000 users plus their posts |
Pattern observation: As the number of users grows, the work grows roughly in proportion to users and their posts.
Time Complexity: O(n + m)
This means the time grows with the number of users (n) plus the total number of posts (m) fetched.
[X] Wrong: "The query time only depends on the number of users requested."
[OK] Correct: Each user's posts add extra work, so total time depends on both users and posts.
Understanding how query time grows with data size shows you can predict performance and design better queries.
"What if we added pagination to posts? How would the time complexity change?"