Out-of-stock page handling in SEO Fundamentals - Time & Space Complexity
When handling out-of-stock pages on a website, it's important to understand how the time to load and process these pages changes as the number of products grows.
We want to know how the website's performance scales when many out-of-stock items are involved.
Analyze the time complexity of the following SEO-related code snippet for handling out-of-stock pages.
// Pseudocode for out-of-stock page handling
function renderOutOfStockPage(products) {
for (let product of products) {
if (product.stock === 0) {
displayMessage(product.name + " is out of stock.");
}
}
}
This code checks each product and shows a message if it is out of stock.
- Primary operation: Looping through the list of products.
- How many times: Once for each product in the list.
As the number of products increases, the time to check each product grows proportionally.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of operations grows directly with the number of products.
Time Complexity: O(n)
This means the time to handle out-of-stock pages grows linearly with the number of products.
[X] Wrong: "Checking out-of-stock status is instant no matter how many products there are."
[OK] Correct: Each product must be checked individually, so more products mean more work and longer time.
Understanding how page handling time grows with product count helps you design better user experiences and scalable websites.
"What if we only checked products flagged as popular instead of all products? How would the time complexity change?"