Supported data types in Firebase - Time & Space Complexity
When working with Firebase, it is important to understand how the types of data you store affect the speed of your operations.
We want to know how the time to save or read data changes as the amount and type of data grows.
Analyze the time complexity of saving different data types to Firebase Firestore.
const docRef = firestore.collection('users').doc('user1');
await docRef.set({
name: 'Alice',
age: 30,
isActive: true,
scores: [10, 20, 30],
address: { city: 'NY', zip: '10001' }
});
This code saves a document with various supported data types like string, number, boolean, array, and object.
Look at what happens when saving data:
- Primary operation: One write API call to save the document.
- How many times: Once per document save, regardless of data types inside.
Saving a document involves one write call no matter how many fields or data types it contains.
| Input Size (n) | Approx. API Calls/Operations |
|---|---|
| 1 field | 1 write call |
| 10 fields | 1 write call |
| 100 fields | 1 write call |
Pattern observation: The number of API calls stays the same even if the data size grows.
Time Complexity: O(1)
This means saving data of any supported type takes a constant number of API calls, no matter the data size.
[X] Wrong: "Saving more fields or complex data types means more API calls and slower performance."
[OK] Correct: Firebase groups all fields in one document write, so the number of API calls stays the same regardless of data complexity.
Understanding how Firebase handles data types and API calls shows you know how cloud databases work efficiently behind the scenes.
"What if we split the data into multiple documents instead of one? How would the time complexity change?"