--- url: "https://web.dev/blog/at-property-performance" titulek: "Benchmarking the performance of CSS @property | Articles | web.dev" h1: "Benchmarking the performance of CSS @property Stay organized with collections Save and categorize content based on your preferences." popis: "What impact does @property have on the performance of your CSS?" jazyk: "en" kanonicka: "https://web.dev/blog/at-property-performance" drobecky: ["Articles", "Benchmarking the performance of CSS @property"] zdroj: "live" wayback_snapshot: "" stazeno: "2026-09-14 02:39:42" znaku: 14553 slov: 2127 kodovani: "iso-8859-2" sha256: "c2ae7b42117e02ca88154a6a6a5439a2bf25989cc335f905f8d30299927a71e8" --- Benchmarking the performance of CSS @property Stay organized with collections Save and categorize content based on your preferences. Bramus Published: Oct 2, 2024 When starting to use a new CSS feature it's important to understand its impact on the performance of your websites, whether positive or negative. With [`@property` now in Baseline](/blog/at-property-baseline) this post explores its performance impact, and things you can do to help prevent negative impact. Benchmarking the performance of CSS with `PerfTestRunner` To benchmark the performance of CSS we built [the "CSS Selector Benchmark" test suite](https://github.com/GoogleChromeLabs/css-selector-benchmark). It is powered by [Chromium's `PerfTestRunner`](https://chromium.googlesource.com/chromium/src/+/refs/heads/main/docs/speed/benchmark/harnesses/blink_perf.md) and benchmarks the performance impact of CSS. This `PerfTestRunner` is what BlinkâChromium's underlying rendering engineâuses for its internal performance tests. The runner includes a `measureRunsPerSecond` method which is used for the tests. The higher the number of runs per second, the better. A basic `measureRunsPerSecond`-benchmark with this library looks like this: `const testResults = PerfTestRunner.measureRunsPerSecond({ "Test Description", iterationCount: 5, bootstrap: function() { // Code to execute before all iterations run // For example, you can inject a style sheet here }, setup: function() { // Code to execute before a single iteration }, run: function() { // The actual test that gets run and measured. // A typical test adjusts something on the page causing a style or layout invalidation }, tearDown: function() { // Code to execute after a single iteration has finished // For example, undo DOM adjustments made within run() }, done: function() { // Code to be run after all iterations have finished. // For example, remove the style sheets that were injected in the bootstrap phase }, });` Each option for `measureRunsPerSecond` is described through comments in the code block, with the `run` function being the core part that gets measured. CSS Selector benchmarks require a DOM tree Because the performance of CSS selectors is also dependent on the size of the DOM, these benchmarks need a decently sized DOM tree. Instead of manually creating this DOM tree, this tree gets generated. For example, the following `makeTree` function is part of the `@property` benchmarks. It constructs a tree of 1000 elements, each element with some children nested inside. `const $container = document.querySelector('#container'); function makeTree(parentEl, numSiblings) { for (var i = 0; i <= numSiblings; i++) { $container.appendChild( createElement('div', { className: `tagDiv wrap${i}`, innerHTML: `
`, }) ); } } makeTree($container, 1000);` Because the CSS selector benchmarks don't modify the DOM tree, this tree generation gets executed only once, before any of the benchmarks run. Running a benchmark To run a benchmark that is part of [the test suite](https://github.com/GoogleChromeLabs/css-selector-benchmark) you must first start a web server: `npm run start` Once started you can visit the benchmark at its published URL and execute `window.startTest()` manually. To run these benchmarks in isolationâwithout any extensions or other factors interveningâ [Puppeteer](https://developer.chrome.com/docs/puppeteer) is [triggered from the CLI](https://github.com/GoogleChromeLabs/css-selector-benchmark/blob/main/cli/index.js) to load and execute the passed in benchmark. For these `@property` benchmarks specifically rather than visiting [the relevant page](https://github.com/GoogleChromeLabs/css-selector-benchmark/blob/main/src/benchmarks/at-rule/at-property.html) at its URL `http://localhost:3000/benchmarks/at-rule/at-property.html` invoke the following commands on the CLI: `npm run benchmark at-rule/at-property` This loads the page through Puppeteer, automatically calls `window.startTest()`, and reports back the results. Benchmarking the performance of CSS properties To benchmark the performance of a CSS property, you benchmark how fast it can handle a [style invalidation](https://chromium.googlesource.com/chromium/src/+/master/third_party/blink/renderer/core/css/style-invalidation.md) and the subsequent recalculate style task the browser needs to do. Style invalidation is the process of marking which elements need their style recalculated in response to a change in the DOM. The simplest possible approach is to invalidate everything in response to every change. When doing so, there is a distinction to make between CSS properties that inherit and CSS properties that don't inherit. When a CSS property that inherits changes on a targeted element, the styles of potentially all elements in the subtree underneath the targeted element also need to change. When a CSS property that doesn't inherit changes on a targeted element, only the styles for that individual element get invalidated. Because it wouldn't be fair to compare properties that do inherit against properties that don't, there are two sets of benchmarks to run: A set of benchmarks with properties that inherit. A set of benchmarks with properties that don't inherit. It's important to carefully choose which properties to benchmark. While some properties (such as `accent-color`) only invalidate styles, there are many properties (such as `writing-mode`) that also invalidate other things such as layout or paint. You want the properties that only invalidate styles. To determine this, look things up in [Blink's list of CSS properties](https://source.chromium.org/chromium/chromium/src/+/main:third_party/blink/renderer/core/css/css_properties.json5). Each property has a field `invalidate` that lists what gets invalidated. Furthermore, it's also important to pick a property that is not marked as `independent` from that list, as benchmarking such a property would skew the results. Independent properties don't have any side-effects on other properties or flags. When only independent properties have changed, Blink uses a fast code-path that clones the style of the descendant and updates the new values in that cloned copy. This approach is faster than doing a full recalculation. Benchmarking the performance of CSS properties that inherit The first set of benchmarks focuses on CSS properties that inherit. There are three types of properties that inherit to test and compare against each other: A regular property that inherits: `accent-color`. An unregistered custom property: `--unregistered`. A custom property that is registered with `inherits: true`: `--registered`. Unregistered custom properties are added to this list because these inherit by default. As mentioned before, the property that inherits was carefully chosen so that it's one that only invalidates styles and one that is not marked as `independent`. As for registered custom properties, only those with the `inherits` descriptor set to true are tested in this run. The `inherits` descriptor determines whether the property inherits down onto children or not. It doesn't matter whether this property is registered through CSS `@property` or JavaScript `CSS.registerProperty`, as the registration itself is not part of the benchmark. The benchmarks As already mentioned, [the page that contains the benchmarks](https://github.com/GoogleChromeLabs/css-selector-benchmark/blob/main/src/benchmarks/at-rule/at-property.html) starts off by constructing a DOM tree so that the page has a big enough set of nodes to see any impact of the changes. Each benchmark changes the value of a property after which it triggers a style invalidation. The benchmark basically measures how long the next recalculation of the page takes to re-evaluate all those invalidated styles. After a single benchmark is done, any injected styles get reset so that the next benchmark can begin. For example, the benchmark measuring the performance of changing the style of `--registered` looks like this: `let i = 0; PerfTestRunner.measureRunsPerSecond({ description, iterationCount: 5, bootstrap: () => { setCSS(`@property --registered { syntax: "