Why push notifications engage users in Firebase - Performance Analysis
We want to understand how the work needed to send push notifications changes as more users get them.
How does the number of users affect the effort to send notifications?
Analyze the time complexity of the following operation sequence.
const messaging = getMessaging();
const tokens = await getUserTokens();
for (const token of tokens) {
await sendPushNotification(messaging, token, messagePayload);
}
This code gets all user tokens and sends a push notification to each user one by one.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Sending a push notification to one user token.
- How many times: Once for each user token in the list.
As the number of users grows, the number of notifications sent grows the same way.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 10 | 10 notifications sent |
| 100 | 100 notifications sent |
| 1000 | 1000 notifications sent |
Pattern observation: The work grows directly with the number of users.
Time Complexity: O(n)
This means sending notifications takes longer as more users get them, growing in a straight line.
[X] Wrong: "Sending one notification automatically sends it to all users at once."
[OK] Correct: Each user needs their own notification sent, so the work adds up with more users.
Understanding how work grows with users helps you design systems that handle many users smoothly.
"What if we send notifications in batches instead of one by one? How would the time complexity change?"