*真的有多慢? [英] How slow is * really?

查看:125
本文介绍了*真的有多慢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每个人都说, * {...} 选择器非常慢。但是它真的有多慢?

Everybody states, that the * { ... } selector is very slow. But how slow is it really?

我总是试图避免它,但有时它是非常有用的。例如: * + h1 {margin-top 1em; } c>


I always try to avoid it, but sometimes it's very useful. For example: * + h1 { margin-top 1em; }

推荐答案

/ code>只有您网页上有元素才会慢。

To put it very simply: the universal selector * is only as slow as there are elements on your page.

由于从右到左匹配的浏览器接受每个元素并匹配所有候选规则,将匹配 * 很好。它本身不会损害性能,但如果你有很多元素在您的页面或一个非常复杂的DOM,这是据称它变慢,但即使这样也不会明显降低浏览器性能。

Since right-to-left matching browsers take each element and match it against all candidate rules, every element will match * 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 元素上测试该选择器,然后检查是否有任何元素出现在它们之前(这真的不需要花费太多的精力,因为 * 基本上是一个保证的匹配)。

Even something like * + 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).

您还可以查看这个我的答案到类似的问题 * + * (这是两个通用选择器!)。

You may also wish to check out this answer of mine to a similar question about * + * (that's two universal selectors!).

这篇关于*真的有多慢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆