Mobile-friendly design in SEO Fundamentals - Time & Space Complexity
When creating mobile-friendly designs, it is important to understand how the page's performance changes as content grows.
We want to know how the design choices affect loading and rendering speed on mobile devices.
Analyze the time complexity of rendering a responsive webpage with multiple CSS media queries.
@media (max-width: 600px) {
.nav-menu { display: none; }
.mobile-menu { display: block; }
}
@media (min-width: 601px) {
.nav-menu { display: block; }
.mobile-menu { display: none; }
}
/* Additional styles for layout and images */
This code snippet switches navigation menus based on screen size to improve mobile usability.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: The browser checks each CSS rule against the screen size to apply styles.
- How many times: Once per CSS rule during page load and on window resize events.
As the number of CSS rules and media queries increases, the browser must evaluate more conditions.
| Input Size (number of CSS rules) | Approx. Operations |
|---|---|
| 10 | 10 checks |
| 100 | 100 checks |
| 1000 | 1000 checks |
Pattern observation: The number of style checks grows directly with the number of CSS rules.
Time Complexity: O(n)
This means the time to apply styles grows in a straight line as you add more CSS rules and media queries.
[X] Wrong: "Adding many media queries won't affect page speed much because CSS is simple."
[OK] Correct: Each media query adds checks the browser must perform, which can slow down rendering especially on mobile devices.
Understanding how CSS rules and media queries affect rendering helps you build efficient mobile-friendly sites and shows you care about user experience and performance.
"What if we combined multiple media queries into one? How would the time complexity change?"