Skip to content
Tutorials Center
My learning

Topics

Lesson 34 of 39 ยท Migration and Best Practices

Sass Performance: Compile Speed and Output Size

Speed up Sass builds and shrink the CSS you ship: use modules, sass-embedded, shallow nesting, mixins over extend, generator limits, gzip measurement and purging unused CSS.

Sass performance has two separate meanings, and people mix them up. Compile speed is how long your build takes; it affects developers. Output size is how many bytes of CSS reach the browser; it affects every visitor, and with it your page speed and search ranking. Different tricks apply to each, and the second matters much more than the first.

This lesson covers both, with a measuring tool you can run in this page, because performance advice without numbers is just superstition.

Compile speed

For most projects Sass compiles in under a second. If yours takes several seconds, check these in order.

  1. Use a fast compiler. sass-embedded is typically several times faster than the pure-JavaScript sass on large projects, and either is fast enough for most sites.
  2. Use @use instead of @import. With @import, a file loaded by 40 components is parsed and evaluated 40 times; with @use it is evaluated once.
  3. Do not import the whole framework. If you use Bootstrap's grid and buttons, load only those parts.
  4. Mind the loops. A generator that produces 20,000 rules (every spacing ร— property ร— breakpoint ร— state) costs both compile time and bytes. Reduce the data.
  5. Watch deep @extend. Extending a popular selector across a big codebase makes the compiler rewrite many selector lists.
  6. Cache and run in parallel. Use the bundler's persistent cache, and the compileAsync API with sass-embedded for many entry files.
  7. Compile in watch mode while developing. Only changed entries rebuild.
  8. Profile before guessing. Time the build (time sass src:dist), then remove half of the @use lines and compare to find the expensive part.

Sass math on a few hundred numbers is cheap. Big maps with thousands of keys and functions that rebuild them on every call (map.merge in a loop is O(n) each time) are the usual compile hogs.

Output size

The browser downloads your CSS, parses it, and matches selectors against the page. Smaller and simpler CSS helps all three steps. These are the levers, from largest to smallest effect.

1. Ship less: purge and split

Generated utilities and frameworks produce megabytes of rules a page never uses. Removing unused CSS is the biggest win:

  • A tool such as PurgeCSS scans your templates and scripts and removes unmatched selectors. Safelist classes added at runtime (is-open, classes built in JavaScript).
  • Split by page or feature: a small critical.css inlined in the head, the rest loaded later; separate bundles for the admin area; print styles only when printing (<link media="print">).
  • Avoid generating variants you do not use (hover versions of every utility, ten shades of every color).

2. Keep generators on a leash

SCSS
// 10 sizes x 4 properties x 5 sides x 5 breakpoints = 1,000 rules

Before adding a generator, estimate the count, and print it while compiling (@debug) to notice growth. Prefer a short scale (six or seven steps) and only the breakpoints that matter.

3. Mixins versus @extend

SCSS
// Mixin: copies declarations into every selector
@mixin card { padding: 1rem; border-radius: 8px; background: white; }
.a { @include card; }
.b { @include card; }

// Extend: one rule with a selector list
%card { padding: 1rem; border-radius: 8px; background: white; }
.a { @extend %card; }
.b { @extend %card; }

@extend gives the smaller uncompressed output, but after gzip or brotli the repeated declarations of the mixin version compress to almost nothing, and the output is easier to read and predict. Use @extend only for constant style bundles and watch for selector lists getting enormous.

4. Nesting depth

Each level of nesting is multiplied into the final selector:

SCSS
.page { .content { .article { .body { p { a { color: red; } } } } } }
// .page .content .article .body p a { color: red; }

Long selectors cost bytes, raise specificity (making overrides harder) and are slower to match. Keep nesting around three levels and use BEM-like classes (BEM and components).

5. Compress and measure

--style=compressed removes whitespace; the web server's gzip or brotli then shrinks the text again. Brotli is usually 15 to 25 percent smaller than gzip for CSS. Always judge by the compressed transfer size, because that is what travels over the network.

Use the tool below to compare two sources. It compiles each with compressed style and measures the real gzip size in your browser.

Measure output size

B is smaller before compression. After gzip the gap nearly disappears for such small input. Add more rules (use copy and paste to reach 50 selectors) and compare again, then try a version of A with a longer, more repetitive mixin.

Selector performance in the browser

Modern engines match selectors extremely fast, so individual selectors rarely matter. These are the ones worth avoiding:

  • Universal or very broad descendant selectors with long chains: .a * .b *.
  • Deep descendant chains that depend on exact markup: they are slow to match and fragile. Prefer one class on the element.
  • Huge selector lists produced by @extend on a core class.

What matters far more is the total amount of CSS, render-blocking requests and large @imported fonts. Measure with the browser's Coverage tool (DevTools: Coverage) to see how much of each stylesheet is used on the page.

Avoid output you do not need

  • Do not emit CSS from abstract files. Anything outside a mixin or function that outputs a selector becomes bytes. Use the 7-1 abstracts rule (architecture).
  • Do not @use a stylesheet that emits CSS from many files if you only need its variables; @use emits it once, but moving variables into a pure tokens file avoids dragging the CSS along.
  • Avoid unused vendor code. Include only what you render.
  • Skip prefixes that your browser targets do not need (let Autoprefixer decide; see bundlers and PostCSS).
  • Use shorthand and CSS variables instead of generating one rule per theme color when the browser can swap values.

A performance checklist

  1. Use @use and sass-embedded.
  2. Compile with compressed for production; serve with brotli.
  3. Count what generators produce; trim the data.
  4. Purge unused CSS and split critical from deferred CSS.
  5. Keep nesting around three levels; prefer classes to long chains.
  6. Measure compressed sizes before and after any "optimization".
  7. Look at DevTools Coverage to find unused rules.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which matters more for visitors: compile time or output size?

    Compile speed affects developers; the CSS that is shipped affects every user.
  2. 2. Why is @use faster than @import in large projects?

    @import re-evaluates the imported file every time.
  3. 3. After gzip, what is usually true of mixin output versus @extend output?

    Compression removes most repeated text, so mixins rarely cost much.
  4. 4. What is the biggest way to cut CSS size from a utility framework?

    Most generated utilities are unused on any given page.
  5. 5. Which DevTools feature shows how much CSS a page actually uses?

    The Coverage panel reports used and unused bytes per stylesheet.

Summary

  • Compile speed: sass-embedded, @use over @import, smaller generators, fewer deep extends, parallel builds.
  • Output size: purge unused CSS, split critical CSS, limit generators, and measure gzip or brotli sizes.
  • Mixins are usually fine after compression; @extend suits constant bundles only.
  • Keep nesting shallow, and keep abstracts free of CSS output.
  • Measure before and after; judge by compressed transfer size and DevTools Coverage.

Next: Best Practices and When to Use Sass