Help us make DevTools Tips better! Fill out this survey to tell us more about your DevTools habits and frustrations.
Find the most expensive CSS selectors
Before rewriting all of your CSS selectors based on some "guidelines" you might have found on the web, it's really important to measure the performance of your webpage first. Don't make your selectors unnecessarily complicated because someone somewhere said that a particular selector was slow. Instead, measure the performance of your page, make sure that CSS is having a negative impact, improve your code, and then measure again!
So, now, how do you know if CSS is having a negative impact, and how to tell which CSS selectors may be at cause?
Microsoft Edge has a very useful new feature starting with Edge version 110 called Selector Stats that can help with this. Here's how to use it:
- Open the Performance tool in Edge.
- Open the Settings of the tool by using the little cog icon in the top-right corner of the tool.
- Check the Enable advanced rendering instrumentation (slow) box.
- Start the perf recording, run through your scenario, and then stop the recording.
- When the profile has loaded, identify the long Recalculate Style blocks (they are purple).
- Click on one of them.
- In the details section below, the Selector Stats tab appears, click on it.
You now have access to the list of CSS selectors that got matched against the DOM tree of the web page during this style recalculation task. You can sort the table by elapsed time or match count to get an understanding of which selectors needed the most time to run.