Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Effect of Many CSS Variables on Performance

This is a somewhat general question.

Is anybody aware of the effects on performance of using many CSS variables within a given document? Has anybody done any tests?

Does the element to which you associate the variable have any effect on performance? Is performance impeded more so by, say, all variables being assigned to :root than assigning them to the individual blocks of styling where they might only be used?

like image 442
oldboy Avatar asked Jun 07 '19 16:06

oldboy


People also ask

Do CSS variables slow down?

Conclusion. Using 5000 CSS variables on 10 000 HTML nodes is only 0.7% slower than raw CSS. You must take into account that this is a very simple usage scenario. From what I see, just by using custom properties, you won't slow down your website rendering significantly.

Is it good to use CSS variables?

CSS variables, more accurately known as CSS custom properties, are landing in Chrome 49. They can be useful for reducing repetition in CSS, and also for powerful runtime effects like theme switching and potentially extending/polyfilling future CSS features.

Should I use Sass variables or CSS variables?

The advantage of CSS variables is undisputed. They can be transformed and overridden whereas SCSS variables can not. CSS variables simplify creating color theme based sites like this right one right here.

Are CSS variables scope?

First of all: CSS variables can have a global or local scope. Global variables can be accessed/used through the entire document, while local variables can be used only inside the selector where it is declared. To create a variable with global scope, declare it inside the :root selector.


1 Answers

Yes there are tests that have been done. Essentially you apply the CSS changes via JavaScript and measure the performance.

You will want to learn about scoping your CSS variables and the number of affected elements. As these numbers increase, so does your draw time.

There is a handy article on this subject at https://lisilinhart.info/posts/css-variables-performance/

TL;DR be aware of style recalculations, since CSS Variables are inheritable — changing a variable on a parent can affect many children prefer using single classes for elements to make style calculations easier for the browser calc() has good performance with variables, but still has problems with browser support with certain units like deg or ms prefer using setProperty rather than inline styles to set CSS variables in JavaScript

And another quote:

Via Javascript the --bg variable was first set on the .container parent element, which resulted in a fairly long duration of 76ms. Then the same variable was set on the first child .el , which only lasted about 1.9ms. So the more children a parent element has using this variable, the more expensive setting a CSS variable on this element gets.

like image 182
Jeff Jenkins Avatar answered Oct 14 '22 00:10

Jeff Jenkins