CSS Performance between class and attribute selectors

前端 未结 4 1500
我寻月下人不归
我寻月下人不归 2021-01-04 07:47

I\'m wondering if there is a performance issue using attribute selectors instead of class selectors.

div.test {}
div[test] {}

P.S. : I\

4条回答
  •  佛祖请我去吃肉
    2021-01-04 08:33

    Someone has actually created a real life selector test that showcases selector matching performance.

    The table outlines that attribute selectors are approximately 3x slower compared to standard classes.

    Relying on attribute selectors also requires more characters to target an element. It's better to define short and concise class names to keep your stylesheet small.

    Example:

    // 17 Characters (attribute)
    [title="example"] {
     ...
    }
    
    // 6 Characters (class)
    .title {
     ...
    }
    

    http://scope.bitbucket.org/tests/selector-matching-performance/

提交回复
热议问题