Title tag optimization in SEO Fundamentals - Time & Space Complexity
When optimizing title tags for SEO, it's important to understand how the time to update or generate these tags grows as the number of pages increases.
We want to know how the work changes when we handle more pages.
Analyze the time complexity of the following code snippet.
// For each page, set a title tag
for (let i = 0; i < pages.length; i++) {
const page = pages[i];
const title = `${page.brand} - ${page.product} | Shop Now`;
document.title = title;
}
This code sets the title tag for each page by combining brand and product names.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Looping through each page to create and set a title tag.
- How many times: Once for every page in the list.
As the number of pages increases, the time to set titles grows directly with it.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 title sets |
| 100 | 100 title sets |
| 1000 | 1000 title sets |
Pattern observation: The work grows evenly as the number of pages grows.
Time Complexity: O(n)
This means the time to optimize title tags increases in direct proportion to the number of pages.
[X] Wrong: "Setting all title tags happens instantly no matter how many pages there are."
[OK] Correct: Each page requires its own title tag update, so more pages mean more work and more time.
Understanding how tasks scale with input size helps you explain your approach clearly and shows you think about efficiency in real projects.
What if we cached the title tags instead of generating them each time? How would the time complexity change?