# The Compositing Phase

> How the browser merges multiple painted layers together using the GPU to create the final image on your screen.

Subject: Internet | Lesson 30 of 35 | 3 min read
Interactive lesson: https://codewithanimation.in/internet/compositing-phase
Previous: [The Paint Phase](https://codewithanimation.in/internet/paint-phase.md) | Next: [Render-Blocking Resources](https://codewithanimation.in/internet/render-blocking-resources.md)

## What it is

Compositing is the final step in the Critical Rendering Path. It takes the visual layers created during the Paint phase and flattens them together into the final image you see on your monitor.

Crucially, Compositing is usually handled by the device's GPU (Graphics Processing Unit), making it incredibly fast compared to CPU-bound Layout and Paint operations.

## Why it matters

If you understand compositing, you unlock 60fps (or 120fps) animations. The golden rule of web performance is to only animate properties that the Compositor can handle alone.

If you only animate `transform` and `opacity`, the browser skips the Layout and Paint phases entirely. The GPU just takes the pre-painted layers, moves them around, or changes their transparency. This is why these two properties are buttery-smooth even on cheap smartphones.

## How it works

When the browser paints elements, it can decide to put certain elements onto their own independent 'Render Layers'.

Think of these layers like sheets of clear acetate stacked on top of each other. If you move one sheet around (`transform`), or make it more transparent (`opacity`), you don't need to redraw the artwork on the sheets themselves. The Compositor just merges the sheets together visually.

## Common mistake: Z-Index and Layer Explosion

A common mistake is forcing too many elements onto their own GPU layers (historically done by adding `transform: translateZ(0)` or `will-change: transform` to everything in hopes of making it 'faster').

Every layer consumes system memory (RAM/VRAM). If you create hundreds of layers, the browser will crash or stutter because the memory overhead destroys any performance gains.

## Tip: Pro tip

**The `will-change` property**: If you have a complex element that you *know* is going to be animated frequently via JavaScript or CSS, give it `will-change: transform`.

This politely asks the browser to put that element on its own compositor layer *before* the animation starts, preventing a sudden lag spike when the animation triggers.

## Try it: Try it

Toggle between animating `margin-left` (CPU) and `transform: translateX` (GPU) to see how the Compositor handles layers independently.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/internet/compositing-phase

## Check yourself

### Question 1

Which two CSS properties are safe to animate because they skip Layout and Paint, going straight to the Compositor?

- A. width and height
- B. transform and opacity
- C. top and left
- D. background-color and color

Correct answer: B

Only `transform` and `opacity` are fully handled by the GPU Compositor without needing to recalculate geometry or redraw pixels.

- Why not A: These trigger Layout reflows.
- Why not C: These trigger Layout reflows.
- Why not D: These trigger Repaints.

### Question 2

What is a major downside of forcing too many elements onto their own compositor layers (e.g., using `will-change: transform` on everything)?

- A. It consumes too much memory (RAM/VRAM) and can crash the browser.
- B. It forces the browser to re-download the HTML.
- C. It breaks JavaScript execution.
- D. It turns off the GPU entirely.

Correct answer: A

Every new layer requires memory to store its painted pixels. Too many layers lead to 'layer explosion', causing severe memory bloat and lag.

- Why not B: Layering is an internal rendering mechanism; it doesn't affect the network.
- Why not C: CSS layers do not stop JS from running.
- Why not D: It overloads the GPU, it doesn't turn it off.

### Question 3

What real-world analogy best describes the Compositing phase?

- A. Calculating the square footage of a room.
- B. Painting the walls of a room.
- C. Stacking transparent sheets of acetate with drawings on them to form a complete picture.
- D. Translating a blueprint into a list of materials.

Correct answer: C

Compositing is literally taking pre-painted, independent layers (like transparent acetate sheets) and stacking them correctly from back to front to create the final screen image.

- Why not A: This represents the Layout phase.
- Why not B: This represents the Paint phase.
- Why not D: This represents Parsing and Tree Construction.

## Remember this

- Compositing merges painted layers together using the GPU.
- Only animate `transform` and `opacity` to hit 60fps.
- Don't abuse `will-change` or create too many layers, as it eats up memory.
