*真的有多慢? [英] How slow is * really?
问题描述
每个人都说, * {...}
选择器非常慢。但是它真的有多慢?
Everybody states, that the * { ... }
selector is very slow. But how slow is it really?
我总是试图避免它,但有时它是非常有用的。例如: I always try to avoid it, but sometimes it's very useful. For example: / code>只有您网页上有元素才会慢。 To put it very simply: the universal selector 由于从右到左匹配的浏览器接受每个元素并匹配所有候选规则,将匹配 Since right-to-left matching browsers take each element and match it against all candidate rules, every element will match 例如, Even something like 您还可以查看这个我的答案到类似的问题 You may also wish to check out this answer of mine to a similar question about 这篇关于*真的有多慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! * + h1 {margin-top 1em; } c>
* + h1 { margin-top 1em; }
推荐答案
*
is only as slow as there are elements on your page. *
很好。它本身不会损害性能,但如果你有很多元素在您的页面或一个非常复杂的DOM,这是据称它变慢,但即使这样也不会明显降低浏览器性能。*
just fine. It by itself doesn't harm performance, but if you have many elements in your page or a very complex DOM, that's where it allegedly gets slow, but even then it doesn't visibly degrade browser performance. * + h1
是合理的,因为如果你想考虑匹配的性能, -left匹配浏览器将首先在 h1
元素上测试该选择器,然后检查是否有任何元素出现在它们之前(这真的不需要花费太多的精力,因为 *
基本上是一个保证的匹配)。* + h1
, for example, is reasonable, since if you want to take matching performance into account, then a right-to-left matching browser will test that selector only on h1
elements first, before checking if there's any element occurring before them (which really doesn't take much effort as *
is basically a guaranteed match). * + *
(这是两个通用选择器!)。* + *
(that's two universal selectors!).