Modular SDK (v9+) tree-shaking in Firebase - Time & Space Complexity
We want to understand how using Firebase's Modular SDK affects the number of operations your app performs.
Specifically, how the code you include changes the work done when your app runs.
Analyze the time complexity of importing and using Firebase services with the Modular SDK.
import { initializeApp } from 'firebase/app';
import { getFirestore, collection, getDocs } from 'firebase/firestore';
const app = initializeApp(firebaseConfig);
const db = getFirestore(app);
const colRef = collection(db, 'users');
const snapshot = await getDocs(colRef);
This code initializes Firebase, accesses Firestore, and fetches documents from a collection.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Fetching documents with
getDocs(). - How many times: Once per collection fetch; each fetch may retrieve multiple documents internally.
As you import only the needed functions, the app loads fewer modules, so startup work stays small.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 document fetch operations inside one getDocs() call |
| 100 | 100 document fetch operations inside one getDocs() call |
| 1000 | 1000 document fetch operations inside one getDocs() call |
Pattern observation: The number of document fetches grows with the number of documents requested, but the imported code size stays minimal due to tree-shaking.
Time Complexity: O(n)
This means the work grows linearly with the number of documents fetched, while unused code is not loaded.
[X] Wrong: "Importing the whole Firebase SDK always loads all services and slows down the app."
[OK] Correct: The Modular SDK lets you import only what you use, so unused parts are left out, keeping the app lean.
Understanding how modular imports affect app performance shows you can write efficient cloud-connected apps that scale well.
What if we imported the entire Firebase SDK instead of just needed functions? How would the time complexity and app size change?