TL;DR: The selector .media__bd--gretting h1{}
is faster.
I say it’s faster based on a series of articles that I’m going to quote here, but it’s worth starting my answer by saying how complicated your question is, and how hard it is to get the jump right when building that answer. In the words of David Hyatt, architect of Safari and Webkit:
The Sad Truth about CSS3 selectors is that they really shouldn’t be used at all if you care about page performance. Decorating your Markup with classes and ids and matching Purely on those while avoiding all uses of sibling, Descendant and Child selectors will Actually make a page perform significantly Better in all browsers.
Which basically means that CSS3 selectors (which encompass your case of the :nth-child()
should not be used if your focus is performance. This quote has been removed of this article, whereas 2009. If you read the article, you will see that the conclusion was that this type of optimization, when performed, results in little gain in speed, and that the cost-benefit ratio is practically zero. If, in 2009, the gain was little, imagine now, in 2015, with technology as a much more efficient whole.
I argued, in that reply, on good CSS practice. One of the points was about the bloating. I have already maintained in code where each element, however repetitive, had its own (and unique), statement. A real example of this is
.bloco1 {
float: left;
width: 33%;
line-height: 1.5;
}
.bloco2 {
float: left;
width: 33%;
line-height: 1.5;
}
and that was repeated by, believe it or not, 12 blocks, all with the same style rules. The end result is a huge and therefore inefficient code bloating. In this case, specifically, it was possible to reduce the CSS
for:
.bloco1, .bloco2{
.
.
.
}
which would already help to decrease the file size and the excess repetitivity in the code. In an ideal world, there would be a class called .bloco
, and Markup would be created accordingly (which was not the case). In this type of situation, you no longer have a Cdisgust STyle SHeet, and you get a STyle SHeet, completely ignoring one of the most powerful Features when it comes to stylization.
This example illustrates that the greatest of the performance problems are not in the depth of specificity of the selector, but rather in a general scope of how great its entire application is, and the time it takes to render. This includes the optimization of everything, HTML
, CSS
and JS
.
That publication (2014), brings with it a series of tests that were done on top of CSS
, and what are the best (and worst) practices. An interesting reading, which leads to a number of nice conclusions, two of them being:
sweating over the selectors used in Modern browsers is futile; Most Selection methods are now so fast it’s really not worth spending Much time over. Furthermore, there is disparity Across browsers of what the slowest selectors are anyway. ...
and
the Battle for high Performing CSS will not be won in the selectors used, it will be won with the judicious use of Property and values
What basically means: Are there faster dials than others? Yes. Sleepless nights are worth it because of it? No. The battle of speed will be won through the intelligence used to build your CSS
(and its application as a whole) and the properties used, not in the milliseconds won if you use a pseudo-selector.
I can be honest, I can’t give the answer, but I think there is a lot of exaggeration about the performance of css selectors, which will define the performance better (I believe) is how many
nodes
you have on the page. It is likely that withnth-child(1)
is slower, but is likely to also take the same time for microsecond perception, the without pseudo can take 1.5ms already the with pseudo can take 2.2ms for example, is so imperceptible that it is a type of optimization that does not compensate. Then I’ll give you +1 on the question because it’s still something interesting.– Guilherme Nascimento