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?
Practice
Solution
Step 1: Understand tree-shaking purpose
Tree-shaking removes unused code to reduce app size and improve performance.Step 2: Apply to Firebase Modular SDK
Using modular imports in Firebase v9+ allows only needed services to be included, enabling tree-shaking.Final Answer:
It reduces app size by including only needed Firebase features. -> Option AQuick Check:
Tree-shaking = smaller app size [OK]
- Thinking tree-shaking adds features automatically
- Confusing tree-shaking with backup or security
- Assuming all Firebase services load by default
Solution
Step 1: Identify modular import syntax
Firebase v9+ uses named imports from specific service packages like 'firebase/firestore'.Step 2: Check each option
import { getFirestore } from 'firebase/firestore'; correctly imports getFirestore from 'firebase/firestore' for modular usage. Others use old or incorrect syntax.Final Answer:
import { getFirestore } from 'firebase/firestore'; -> Option BQuick Check:
Modular import = named import from service path [OK]
- Using default import from 'firebase' package
- Importing from 'firebase/app' instead of service path
- Using wildcard imports that prevent tree-shaking
import { initializeApp } from 'firebase/app';
import { getAuth } from 'firebase/auth';
const app = initializeApp({ apiKey: 'abc', authDomain: 'xyz' });
const auth = getAuth(app);
console.log(typeof auth.currentUser);Solution
Step 1: Understand getAuth and currentUser
getAuth returns an Auth instance; currentUser is a property that is either null or a user object.Step 2: Check typeof currentUser before login
Before login, currentUser is null, and typeof null is 'object' in JavaScript.Final Answer:
'object' -> Option DQuick Check:
typeof null = 'object' in JS [OK]
- Assuming currentUser is undefined before login
- Confusing typeof null with 'null' string
- Thinking currentUser is a function
import { initializeApp } from 'firebase/app';
import { getFirestore } from 'firebase/firestore';
const app = initializeApp(config);
const db = getFirestore();Solution
Step 1: Check usage of initializeApp
initializeApp requires a config object to initialize Firebase app.Step 2: Verify presence of config object
The code uses 'config' but does not define or import it, causing a ReferenceError.Final Answer:
config object is missing from the code. -> Option CQuick Check:
Missing config causes initialization error [OK]
- Forgetting to pass app to getFirestore (optional but recommended)
- Importing initializeApp from wrong package
- Calling getFirestore before app initialization
Solution
Step 1: Use modular imports from correct packages
Authentication and Firestore must be imported from 'firebase/auth' and 'firebase/firestore' respectively for tree-shaking.Step 2: Initialize app and pass it to service getters
Initialize app with config, then pass app instance to getAuth and getFirestore functions.Step 3: Check each option
import { initializeApp } from 'firebase/app'; import { getAuth } from 'firebase/auth'; import { getFirestore } from 'firebase/firestore'; const app = initializeApp(config); const auth = getAuth(app); const db = getFirestore(app); correctly imports and initializes services modularly. Others use old syntax or wrong imports preventing tree-shaking.Final Answer:
Option A code snippet -> Option AQuick Check:
Modular imports + app passed = optimized bundle [OK]
- Using default firebase import disables tree-shaking
- Importing multiple services from 'firebase/app' only
- Not passing app instance to service getters
Ready to prove it?
Pass the test and this pattern moves unknown
