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?"
Practice
Solution
Step 1: Review Firebase supported data types
Firebase supports strings, booleans, numbers, arrays, maps, timestamps, and geopoints.Step 2: Identify unsupported type
Functions are not stored as data types in Firestore; they are part of code logic, not data.Final Answer:
Function -> Option BQuick Check:
Functions are not data types in Firestore [OK]
- Confusing code functions with data types
- Thinking all JavaScript types are supported
- Assuming Firestore stores executable code
Solution
Step 1: Understand Firestore timestamp storage
Firestore has a special Timestamp type to store date and time accurately.Step 2: Compare options
Strings and numbers can store dates but lack Firestore's timestamp features; booleans are unrelated.Final Answer:
Use the Firebase Timestamp type -> Option CQuick Check:
Timestamp type stores dates best [OK]
- Storing dates as plain strings
- Using numbers without Timestamp type
- Confusing booleans with dates
location after this Firestore document is saved?const docData = { location: new firebase.firestore.GeoPoint(40.7128, -74.0060) };Solution
Step 1: Identify the data type used
The code usesnew firebase.firestore.GeoPoint()which creates a GeoPoint object.Step 2: Understand Firestore storage
Firestore stores GeoPoint objects as GeoPoint type, representing geographic coordinates.Final Answer:
GeoPoint -> Option AQuick Check:
GeoPoint stores latitude and longitude [OK]
- Thinking GeoPoint is stored as string
- Assuming coordinates are arrays
- Confusing GeoPoint with map type
{ name: 'Alice', age: '25' }But you want
age to be a number. What is the error and how to fix it?Solution
Step 1: Check data types in the object
Age is stored as a string '25', not a number 25.Step 2: Understand Firestore type strictness
Firestore stores data as given; it does not convert string to number automatically.Final Answer:
Error: age is string, fix by storing age as number 25 -> Option DQuick Check:
Store numbers as numbers, not strings [OK]
- Assuming Firestore auto-converts types
- Using booleans for numbers
- Ignoring type mismatch warnings
location and signupDate fields?Solution
Step 1: Identify best types for location and date
Firestore provides GeoPoint for geographic coordinates and Timestamp for dates.Step 2: Evaluate options
Using GeoPoint and Timestamp ensures correct storage, querying, and indexing.Final Answer:
location as GeoPoint, signupDate as Timestamp -> Option AQuick Check:
Use GeoPoint and Timestamp for location and dates [OK]
- Storing dates as strings
- Using arrays for location
- Confusing booleans with dates
Ready to prove it?
Pass the test and this pattern moves unknown
