0
0
Firebasecloud~5 mins

Authentication trigger functions in Firebase - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Authentication trigger functions
O(n)
Understanding Time Complexity

When using authentication trigger functions in Firebase, it's important to understand how the time to run these functions changes as more users sign up or log in.

We want to know how the function's work grows when many users trigger it.

Scenario Under Consideration

Analyze the time complexity of the following Firebase authentication trigger function.

exports.onUserCreate = functions.auth.user().onCreate((user) => {
  const userId = user.uid;
  return admin.firestore().collection('profiles').doc(userId).set({
    email: user.email,
    createdAt: admin.firestore.FieldValue.serverTimestamp()
  });
});

This function runs when a new user signs up. It creates a profile document in Firestore for that user.

Identify Repeating Operations

Look for repeated work inside the function.

  • Primary operation: Writing one document to Firestore per user creation.
  • How many times: Once per user sign-up event.
How Execution Grows With Input

Each new user triggers one write operation to Firestore.

Input Size (n)Approx. Operations
1010 writes
100100 writes
10001000 writes

Pattern observation: The work grows directly with the number of users signing up.

Final Time Complexity

Time Complexity: O(n)

This means the time to handle all sign-ups grows linearly with the number of new users.

Common Mistake

[X] Wrong: "The function runs once and handles all users at once."

[OK] Correct: Each user sign-up triggers the function separately, so the work adds up with each new user.

Interview Connect

Understanding how trigger functions scale helps you design systems that handle many users smoothly and predict how your app behaves as it grows.

Self-Check

What if the function also read multiple documents from Firestore for each new user? How would the time complexity change?