Notification handling in foreground in Firebase - Time & Space Complexity
When an app is open, it listens for notifications to show them immediately. We want to see how the work needed grows as more notifications arrive.
How does the app handle more notifications without slowing down?
Analyze the time complexity of the following operation sequence.
// Listen for messages when app is in foreground
firebase.messaging().onMessage((payload) => {
console.log('Message received. ', payload);
// Show notification using browser API
new Notification(payload.notification.title, {
body: payload.notification.body
});
});
This code listens for each incoming notification and shows it immediately to the user.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Receiving each notification message and creating a new notification display.
- How many times: Once per notification received while app is open.
Each new notification triggers one display operation. More notifications mean more calls, growing directly with the number of messages.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 notification displays |
| 100 | 100 notification displays |
| 1000 | 1000 notification displays |
Pattern observation: The work grows in a straight line with the number of notifications.
Time Complexity: O(n)
This means the time to handle notifications grows directly with how many notifications arrive.
[X] Wrong: "Handling one notification means all notifications are handled instantly together."
[OK] Correct: Each notification triggers its own handling separately, so time grows with the number of notifications, not all at once.
Understanding how notification handling scales helps you design apps that stay smooth even with many messages. This skill shows you can think about user experience and app performance together.
"What if we batch notifications and show them all at once? How would the time complexity change?"