Optimistic UI updates in GraphQL - Time & Space Complexity
When using optimistic UI updates, we want to know how the work grows as more data changes happen.
How does the system handle many updates quickly and what costs come with it?
Analyze the time complexity of this optimistic update GraphQL mutation.
mutation AddComment($postId: ID!, $text: String!) {
addComment(postId: $postId, text: $text) {
id
text
author {
id
name
}
}
}
This mutation adds a comment and the UI updates immediately before server response.
Look for repeated work when many optimistic updates happen.
- Primary operation: Updating the local cache or UI state for each new comment.
- How many times: Once per comment added optimistically, so it grows with the number of comments.
Each new comment triggers a cache update and UI refresh.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 cache updates and UI refreshes |
| 100 | 100 cache updates and UI refreshes |
| 1000 | 1000 cache updates and UI refreshes |
Pattern observation: The work grows directly with the number of optimistic updates.
Time Complexity: O(n)
This means the time to update the UI grows in a straight line as more updates happen.
[X] Wrong: "Optimistic updates happen instantly with no cost no matter how many."
[OK] Correct: Each update changes the UI and cache, so more updates mean more work and time.
Understanding how optimistic UI updates scale shows you can balance fast user feedback with system performance.
What if we batch multiple optimistic updates together? How would the time complexity change?