Bird
Raised Fist0
SASSmarkup~8 mins

Design token management in SASS - Performance & Optimization

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Performance: Design token management
MEDIUM IMPACT
Design token management affects CSS generation and runtime style recalculations, impacting page load speed and rendering consistency.
Using design tokens for consistent theming in a large project
SASS
$colors: (
  primary: #007bff,
  secondary: #6c757d
);

@function color($name) {
  @return map-get($colors, $name);
}

.button {
  color: color(primary);
  background-color: color(secondary);
}

// Centralized tokens with map and function
Centralizing tokens in a map and using functions avoids duplication and keeps CSS smaller and consistent.
📈 Performance Gainreduces CSS bundle size by 20-50kb, improves maintainability and reduces layout shifts
Using design tokens for consistent theming in a large project
SASS
$primary-color: #007bff;
$secondary-color: #6c757d;
$primary-color: #0056b3; // redefining tokens multiple times

.button {
  color: $primary-color;
  background-color: $secondary-color;
}

// Tokens scattered and redefined in many files
Redefining tokens multiple times and scattering them causes larger CSS output and inconsistent styles, leading to more CSS to parse and potential layout shifts.
📉 Performance Costadds 20-50kb to CSS bundle, increases style recalculation on theme changes
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Scattered and redefined tokensNo direct DOM impactTriggers multiple style recalculationsHigher paint cost due to inconsistent styles[X] Bad
Centralized tokens with Sass maps and functionsNo direct DOM impactSingle style calculation on loadLower paint cost with consistent styles[OK] Good
Rendering Pipeline
Design tokens are compiled into CSS during build time, affecting style calculation and layout stages in the browser. Efficient token management reduces CSS size and complexity, speeding up style calculation and paint.
Style Calculation
Layout
Paint
⚠️ BottleneckStyle Calculation
Core Web Vital Affected
LCP
Design token management affects CSS generation and runtime style recalculations, impacting page load speed and rendering consistency.
Optimization Tips
1Centralize design tokens in Sass maps to avoid duplication.
2Use functions to access tokens for consistent and maintainable styles.
3Avoid redefining tokens multiple times to reduce CSS size and style recalculations.
Performance Quiz - 3 Questions
Test your performance knowledge
How does centralizing design tokens in Sass maps improve performance?
AIt reduces CSS bundle size and avoids duplicated styles.
BIt increases CSS specificity for better overrides.
CIt forces the browser to recalculate styles more often.
DIt adds more CSS files to the project.
DevTools: Performance
How to check: Record a performance profile while loading the page and inspect the 'Style Recalculation' events to see if multiple recalculations occur due to CSS complexity.
What to look for: Look for fewer and shorter 'Style Recalculation' events indicating efficient CSS and token usage.

Practice

(1/5)
1. What is the main purpose of design tokens in Sass?
easy
A. To write JavaScript functions inside Sass
B. To create animations in Sass
C. To store reusable style values like colors and sizes
D. To manage HTML structure

Solution

  1. Step 1: Understand design tokens concept

    Design tokens are variables that hold style values such as colors, fonts, and sizes.
  2. Step 2: Identify their purpose in Sass

    They help keep styles consistent and easy to update by reusing these values.
  3. Final Answer:

    To store reusable style values like colors and sizes -> Option C
  4. Quick Check:

    Design tokens = reusable style values [OK]
Hint: Design tokens store style values for reuse [OK]
Common Mistakes:
  • Thinking design tokens are for animations
  • Confusing design tokens with JavaScript code
  • Believing design tokens manage HTML
2. Which of the following is the correct way to declare a design token for a primary color in Sass?
easy
A. let $primary-color = #3498db;
B. primary-color = #3498db;
C. var primary-color = #3498db;
D. $primary-color: #3498db;

Solution

  1. Step 1: Recall Sass variable syntax

    Sass variables start with a dollar sign ($) and use a colon to assign values.
  2. Step 2: Check each option

    $primary-color: #3498db; uses correct Sass syntax: $primary-color: #3498db;. Others use JavaScript or invalid syntax.
  3. Final Answer:

    $primary-color: #3498db; -> Option D
  4. Quick Check:

    Sass variables start with $ and use : [OK]
Hint: Sass variables start with $ and use colon : [OK]
Common Mistakes:
  • Using JavaScript variable syntax in Sass
  • Omitting the $ sign before variable name
  • Using = instead of : for assignment
3. Given the Sass code:
$font-size-base: 1.6rem;
$font-size-large: $font-size-base * 1.5;

body {
  font-size: $font-size-large;
}

What will be the computed font size for the body element?
medium
A. 2.4rem
B. 1.5rem
C. 1.6rem
D. 3.1rem

Solution

  1. Step 1: Calculate $font-size-large value

    $font-size-large is $font-size-base multiplied by 1.5, so 1.6rem * 1.5 = 2.4rem.
  2. Step 2: Apply value to body font-size

    The body font-size uses $font-size-large, so it will be 2.4rem.
  3. Final Answer:

    2.4rem -> Option A
  4. Quick Check:

    1.6rem * 1.5 = 2.4rem [OK]
Hint: Multiply base size by factor for large size [OK]
Common Mistakes:
  • Using 1.5rem instead of multiplying
  • Confusing base size with large size
  • Forgetting to multiply the values
4. Identify the error in this Sass code for managing design tokens:
$color-primary: #ff0000
$color-secondary: #00ff00;

.button {
  background-color: $color-primary;
}
medium
A. Missing semicolon after $color-primary declaration
B. Wrong variable name syntax
C. Using hex colors is not allowed in Sass variables
D. Background color property is invalid

Solution

  1. Step 1: Check variable declarations

    $color-primary is missing a semicolon at the end of the line, which is required in Sass.
  2. Step 2: Verify other parts

    Variable names and background-color property are correct. Hex colors are valid.
  3. Final Answer:

    Missing semicolon after $color-primary declaration -> Option A
  4. Quick Check:

    Each Sass variable line must end with ; [OK]
Hint: Always end Sass variable lines with semicolon ; [OK]
Common Mistakes:
  • Omitting semicolons after variable declarations
  • Thinking hex colors are invalid in Sass
  • Misnaming variables without $ sign
5. You want to create a design token system in Sass that allows easy theme switching between light and dark modes. Which approach below best manages color tokens for this purpose?
hard
A. Hardcode colors directly in CSS without variables
B. Define separate maps for light and dark colors, then use a variable to select the active map
C. Use JavaScript to change colors only, ignoring Sass variables
D. Create one set of variables and manually change each color in the stylesheet

Solution

  1. Step 1: Understand theme switching needs

    Theme switching requires grouping colors so you can easily swap them based on mode.
  2. Step 2: Evaluate options for managing tokens

    Using separate Sass maps for light and dark themes allows selecting the active theme map with a variable, making switching easy and maintainable.
  3. Step 3: Reject other options

    Hardcoding colors or manual changes are error-prone and not scalable. JavaScript alone ignores Sass benefits.
  4. Final Answer:

    Define separate maps for light and dark colors, then use a variable to select the active map -> Option B
  5. Quick Check:

    Use maps and variables for theme switching [OK]
Hint: Use Sass maps and a variable to switch themes [OK]
Common Mistakes:
  • Hardcoding colors instead of using variables
  • Ignoring Sass variables and relying only on JavaScript
  • Manually changing colors everywhere instead of grouping