0
0
Firebasecloud~5 mins

Notification handling in foreground in Firebase - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Notification handling in foreground
O(n)
Understanding Time 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?

Scenario Under Consideration

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 Repeating Operations

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.
How Execution Grows With Input

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
1010 notification displays
100100 notification displays
10001000 notification displays

Pattern observation: The work grows in a straight line with the number of notifications.

Final Time Complexity

Time Complexity: O(n)

This means the time to handle notifications grows directly with how many notifications arrive.

Common Mistake

[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.

Interview Connect

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.

Self-Check

"What if we batch notifications and show them all at once? How would the time complexity change?"