Performance monitoring in Firebase - Time & Space Complexity
Performance monitoring in Firebase helps track how your app behaves over time.
We want to understand how the cost of monitoring grows as your app handles more data or users.
Analyze the time complexity of the following Firebase performance monitoring setup.
import { getPerformance, trace } from "firebase/performance";
const perf = getPerformance();
const myTrace = trace(perf, "custom_trace");
myTrace.start();
// ... some app code ...
myTrace.stop();
This code starts and stops a custom performance trace to measure a specific part of the app.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Starting and stopping a trace around app code.
- How many times: Each trace runs once per monitored event or user action.
Each trace measures one event, so the cost grows with the number of events monitored.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 events | 10 trace start/stop operations |
| 100 events | 100 trace start/stop operations |
| 1000 events | 1000 trace start/stop operations |
Pattern observation: The number of operations grows directly with the number of events monitored.
Time Complexity: O(n)
This means the monitoring cost grows linearly as you track more events.
[X] Wrong: "Performance monitoring runs once and covers all events automatically."
[OK] Correct: Each trace must be started and stopped around specific events, so cost grows with how many you track.
Understanding how monitoring scales helps you design apps that stay fast and reliable as they grow.
"What if we added nested traces inside other traces? How would the time complexity change?"
Practice
Solution
Step 1: Understand the role of performance monitoring
Performance monitoring is used to check how fast an app runs and find parts that slow it down.Step 2: Compare with other Firebase features
Features like data storage, notifications, and authentication serve different purposes unrelated to speed tracking.Final Answer:
To track app speed and identify slow parts -> Option BQuick Check:
Performance monitoring = track speed [OK]
- Confusing performance monitoring with data storage
- Thinking it manages user authentication
- Assuming it sends notifications
Solution
Step 1: Recall the official Firebase Performance Monitoring initialization
The correct method is to callgetPerformance(app)to start monitoring.Step 2: Check other options for correctness
Other options use incorrect or outdated function names that do not exist in Firebase SDK.Final Answer:
const perf = getPerformance(app); -> Option CQuick Check:
Use getPerformance(app) to start monitoring [OK]
- Using non-existent functions like initializePerformance()
- Calling firebase.performance() which is outdated
- Confusing function names with other Firebase services
const perf = getPerformance(app);
const trace = perf.trace('myTrace');
trace.start();
// some code
trace.stop();
console.log(trace.name);What will be printed in the console?
Solution
Step 1: Understand the trace creation and naming
The trace is created with the name 'myTrace', so itsnameproperty holds 'myTrace'.Step 2: Check the console.log output
Loggingtrace.nameprints the string 'myTrace' to the console.Final Answer:
'myTrace' -> Option DQuick Check:
trace.name = 'myTrace' [OK]
- Assuming trace.name is undefined or missing
- Confusing trace object with string 'trace'
- Expecting an error from accessing trace.name
const perf = getPerformance(app);
const trace = perf.trace('loadTime');
trace.start;
// some loading code
trace.stop();Why does the trace not record the duration correctly?
Solution
Step 1: Check how trace.start is used
The code usestrace.start;without parentheses, so the function is not executed.Step 2: Understand function call syntax
To start the trace,trace.start()must be called with parentheses to run the function.Final Answer:
Because trace.start is missing parentheses and not called as a function -> Option AQuick Check:
trace.start() needs parentheses to run [OK]
- Forgetting parentheses on start() call
- Calling stop() before start()
- Thinking trace name affects recording
- Misplacing getPerformance call
Solution
Step 1: Understand how to create separate traces
Each function should have its own trace object created with a unique name.Step 2: Check the usage of start and stop for each trace
Start and stop must be called on each trace around the respective function call.Step 3: Analyze each option
const perf = getPerformance(app); const traceA = perf.trace('funcA'); const traceB = perf.trace('funcB'); traceA.start(); funcA(); traceA.stop(); traceB.start(); funcB(); traceB.stop(); correctly creates two traces and starts/stops them separately. const perf = getPerformance(app); const trace = perf.trace('funcA_funcB'); trace.start(); funcA(); funcB(); trace.stop(); uses one trace for both functions, losing separate timing. const perf = getPerformance(app); perf.trace('funcA').start(); funcA(); perf.trace('funcA').stop(); perf.trace('funcB').start(); funcB(); perf.trace('funcB').stop(); calls start and stop on new trace objects without saving them, which is incorrect. const perf = getPerformance(app); const traceA = perf.trace('funcA'); traceA.start(); funcA(); traceA.stop(); traceA.start(); funcB(); traceA.stop(); reuses the same trace for two functions, which mixes timings.Final Answer:
const perf = getPerformance(app); const traceA = perf.trace('funcA'); const traceB = perf.trace('funcB'); traceA.start(); funcA(); traceA.stop(); traceB.start(); funcB(); traceB.stop(); -> Option AQuick Check:
Separate trace per function = const perf = getPerformance(app); const traceA = perf.trace('funcA'); const traceB = perf.trace('funcB'); traceA.start(); funcA(); traceA.stop(); traceB.start(); funcB(); traceB.stop(); [OK]
- Using one trace for multiple functions
- Not saving trace objects before start/stop
- Reusing the same trace for different functions
Ready to prove it?
Pass the test and this pattern moves unknown
