Bundle preloading in Firebase - Time & Space Complexity
We want to understand how the time to preload bundles grows as we add more bundles to preload in Firebase hosting.
Specifically, how does the number of bundles affect the number of preload operations?
Analyze the time complexity of the following operation sequence.
const bundles = ["bundle1.js", "bundle2.js", "bundle3.js"];
bundles.forEach(bundle => {
firebase.hosting.preloadBundle(bundle);
});
This code preloads each JavaScript bundle one by one using Firebase Hosting's preload API.
Identify the API calls, resource provisioning, data transfers that repeat.
- Primary operation: Calling
firebase.hosting.preloadBundle()for each bundle. - How many times: Once per bundle in the list.
Each additional bundle adds one more preload call, so the total calls grow directly with the number of bundles.
| Input Size (n) | Approx. Api Calls/Operations |
|---|---|
| 10 | 10 preload calls |
| 100 | 100 preload calls |
| 1000 | 1000 preload calls |
Pattern observation: The number of preload operations grows linearly as the number of bundles increases.
Time Complexity: O(n)
This means the time to preload bundles grows in direct proportion to how many bundles you have.
[X] Wrong: "Preloading many bundles happens all at once and takes the same time as preloading one."
[OK] Correct: Each bundle requires a separate preload call, so more bundles mean more calls and more time.
Understanding how preload operations scale helps you design efficient loading strategies and shows you can think about resource costs clearly.
"What if we preload bundles in parallel instead of one by one? How would the time complexity change?"
Practice
Solution
Step 1: Understand bundle preloading concept
Bundle preloading downloads code bundles early before they are needed to speed up app loading.Step 2: Identify the main benefit
This early download improves user experience by reducing wait time when features are accessed.Final Answer:
To download important code bundles early for faster app loading -> Option AQuick Check:
Bundle preloading = early download for speed [OK]
- Thinking preloading delays code loading
- Confusing preloading with disabling code splitting
- Assuming preloading increases bundle size
firebase.json under hosting?Solution
Step 1: Review JSON array syntax for preloadBundles
The correct syntax uses an array of strings listing bundle filenames.Step 2: Identify correct JSON format
"preloadBundles": ["bundle1.js", "bundle2.js"] correctly uses an array of quoted strings, which is valid JSON.Final Answer:
"preloadBundles": ["bundle1.js", "bundle2.js"] -> Option CQuick Check:
Preload bundles = array of strings [OK]
- Using unquoted bundle names causing syntax errors
- Passing bundles as a single string instead of array
- Using object instead of array for bundles
firebase.json snippet:{
"hosting": {
"preloadBundles": ["main.js", "feature.js"]
}
}What happens when a user visits the site?
Solution
Step 1: Understand preloadBundles effect
Bundles listed in preloadBundles start downloading early when the site loads.Step 2: Apply to given bundles
Both 'main.js' and 'feature.js' will preload to improve speed when features are used.Final Answer:
Both 'main.js' and 'feature.js' start downloading early to speed up usage -> Option BQuick Check:
Preload means early download of all listed bundles [OK]
- Assuming only main bundle preloads
- Thinking bundles load only after user action
- Believing preload order is reversed
preloadBundles in firebase.json as:{
"hosting": {
"preloadBundles": ["app.js", app2.js"]
}
}What is the problem with this configuration?
Solution
Step 1: Check JSON syntax for bundle names
All bundle names must be strings enclosed in double quotes.Step 2: Identify error in given config
'app2.js' is missing quotes, causing invalid JSON syntax.Final Answer:
Missing quotes around 'app2.js' causes JSON syntax error -> Option DQuick Check:
JSON strings need quotes [OK]
- Forgetting quotes around bundle names
- Placing preloadBundles outside hosting
- Using objects instead of string arrays
firebase.json to optimize loading?Solution
Step 1: Understand preload strategy
Preloading bundles that users access often improves perceived speed without wasting bandwidth.Step 2: Apply selective preloading
Only list frequently used bundles in preloadBundles to optimize loading and avoid unnecessary downloads.Final Answer:
List only frequently used bundles inpreloadBundlesto download early -> Option AQuick Check:
Preload important bundles only for best performance [OK]
- Preloading all bundles causing slow initial load
- Ignoring usage patterns when preloading
- Preloading large unused bundles wasting resources
Ready to prove it?
Pass the test and this pattern moves unknown
