简体   繁体   中英

Vuejs css scoped performance

I'm developing a new app with VueJs and I see that implement a "css scoped" like this

<style scoped>
.example {
  color: red;
}
</style>

<template>
  <div class="example">hi</div>
</template>

It render like

<style>
.example[_v-f3f3eg9] {
  color: red;
}
</style>

<template>
  <div class="example" _v-f3f3eg9>hi</div>
</template>

I'm going to develop a big project with many components in Atomic design and I'm asking if it's better, for performance, to use classes or use scoped

Scoped styles do not eliminate the need for classes . Due to the way browsers render various CSS selectors, p { color: red } will be many times slower when scoped (ie when combined with an attribute selector). If you use classes or ids instead, such as in .example { color: red }, then you virtually eliminate that performance hit. Here's a playground where you can test the differences yourself.

This is to quote the official vue-loader documentation that can be found here

It will not affect the performance.

The css parsing time will be absolutely insignificant in comparison more complicated processes (for example, parsing/interpreting js, rendering html, etc).

I think it's dangerous to assume that attribute selectors won't affect performance from a CSSOM perspective. Doing a quick search, I can't find much to back this up, but it seems like Vue's intentions are great, but the end result could potentially release far less performant CSS than if they did not do this with attribute selectors.

I think it's something that depending on the size of your app, you'll just need to benchmark. As a baseline though, you should not want to use attribute selectors given they are less specific, thus slightly more effort for the browser to target.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM