Page speed fundamentals in SEO Fundamentals - Time & Space Complexity
Page speed is about how fast a web page loads and becomes usable. Analyzing time complexity here helps us understand what parts of the page affect loading time as the page grows.
We want to know how the loading time changes when the page has more content or elements.
Analyze the time complexity of the following HTML and CSS structure affecting page speed.
<ul class="menu">
<li>Home</li>
<li>About</li>
<li>Services</li>
<li>Contact</li>
</ul>
.menu li {
color: black;
font-size: 1rem;
}
This code creates a menu list with several items styled by a simple CSS selector.
Look at what repeats when the page loads or styles are applied.
- Primary operation: The browser applies styles to each <li> item in the list.
- How many times: Once for each list item, so if there are n items, it happens n times.
As the number of list items grows, the browser must apply styles to more elements.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 style applications |
| 100 | 100 style applications |
| 1000 | 1000 style applications |
Pattern observation: The work grows directly with the number of elements; doubling elements doubles the work.
Time Complexity: O(n)
This means the time to apply styles grows linearly with the number of elements on the page.
[X] Wrong: "Adding more list items won't affect page speed much because CSS is fast."
[OK] Correct: Even simple CSS selectors must be applied to each element, so more items mean more work and slower rendering.
Understanding how page elements affect loading time helps you build faster websites and shows you can think about performance in real projects.
"What if we changed the CSS selector from '.menu li' to a more complex one like '.menu > li > a'? How would the time complexity change?"