Bird
Raised Fist0
Angularframework~8 mins

Virtual scrolling for large lists in Angular - 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: Virtual scrolling for large lists
HIGH IMPACT
Virtual scrolling improves page load speed and rendering performance by only rendering visible list items instead of the entire large list.
Displaying a large list of items in a scrollable container
Angular
import { Component } from '@angular/core';
import { CdkVirtualScrollViewport } from '@angular/cdk/scrolling';
@Component({
  selector: 'app-virtual-list',
  template: `<cdk-virtual-scroll-viewport itemSize="40" style="height:400px;">
    <div *cdkVirtualFor="let item of items">{{item}}</div>
  </cdk-virtual-scroll-viewport>`
})
export class VirtualListComponent {
  items = Array.from({length: 10000}, (_, i) => `Item ${i + 1}`);
}
Only renders visible items plus a small buffer, drastically reducing DOM nodes and reflows.
📈 Performance GainTriggers about 10-20 reflows instead of 10,000, improving LCP and reducing memory use.
Displaying a large list of items in a scrollable container
Angular
import { Component } from '@angular/core';
@Component({
  selector: 'app-large-list',
  template: `<div style='height:400px; overflow:auto;'>
    <div *ngFor='let item of items'>{{item}}</div>
  </div>`
})
export class LargeListComponent {
  items = Array.from({length: 10000}, (_, i) => `Item ${i + 1}`);
}
Renders all 10,000 items at once, creating a huge DOM tree and causing slow initial load and heavy memory use.
📉 Performance CostTriggers 10,000 reflows and paints, blocking rendering for hundreds of milliseconds.
Performance Comparison
PatternDOM OperationsReflowsPaint CostVerdict
Rendering full list10,000 nodes created10,000 reflowsHigh paint cost[X] Bad
Virtual scrolling20-30 nodes created20 reflowsLow paint cost[OK] Good
Rendering Pipeline
Virtual scrolling limits the number of DOM nodes by rendering only visible items. This reduces the work in Style Calculation, Layout, and Paint stages.
Layout
Paint
Composite
⚠️ BottleneckLayout is most expensive due to fewer elements needing size and position calculation.
Core Web Vital Affected
LCP
Virtual scrolling improves page load speed and rendering performance by only rendering visible list items instead of the entire large list.
Optimization Tips
1Render only visible list items to reduce DOM size.
2Use Angular CDK virtual scroll for efficient large lists.
3Avoid rendering thousands of elements at once to prevent layout thrashing.
Performance Quiz - 3 Questions
Test your performance knowledge
What is the main performance benefit of using virtual scrolling for large lists?
ARendering only visible items reduces DOM size and speeds up rendering
BIt preloads all items to avoid delays during scrolling
CIt uses CSS animations to improve scrolling smoothness
DIt caches all list items in memory for faster access
DevTools: Performance
How to check: Record a performance profile while scrolling the list. Observe the number of layout and paint events triggered.
What to look for: Look for fewer layout recalculations and shorter long tasks indicating smoother scrolling and faster rendering.

Practice

(1/5)
1. What is the main benefit of using cdk-virtual-scroll-viewport in Angular for large lists?
easy
A. It renders only visible items to improve performance.
B. It automatically sorts the list items alphabetically.
C. It adds animations to list items when scrolling.
D. It duplicates list items for faster loading.

Solution

  1. Step 1: Understand virtual scrolling purpose

    Virtual scrolling aims to improve performance by rendering only the visible items in a large list.
  2. Step 2: Identify cdk-virtual-scroll-viewport role

    This Angular component implements virtual scrolling by showing only the items visible in the viewport, reducing DOM load.
  3. Final Answer:

    It renders only visible items to improve performance. -> Option A
  4. Quick Check:

    Virtual scrolling = render visible items only [OK]
Hint: Virtual scroll shows visible items only to boost speed [OK]
Common Mistakes:
  • Thinking it sorts or animates items
  • Assuming it duplicates items for speed
  • Confusing with pagination
2. Which of the following is the correct way to use virtual scrolling in Angular template?
easy
A. <cdk-virtual-scroll-viewport><div *cdkVirtualFor="let item in items">{{item}}</div></cdk-virtual-scroll-viewport>
B. <cdk-virtual-scroll-viewport itemSize="50"><div *ngFor="let item of items">{{item}}</div></cdk-virtual-scroll-viewport>
C. <cdk-virtual-scroll-viewport itemSize="50"><div *cdkVirtualFor="let item of items">{{item}}</div></cdk-virtual-scroll-viewport>
D. <cdk-virtual-scroll-viewport itemSize="50"><div *ngFor="let item in items">{{item}}</div></cdk-virtual-scroll-viewport>

Solution

  1. Step 1: Identify correct directive for virtual scroll

    Angular CDK virtual scroll requires *cdkVirtualFor instead of *ngFor to render visible items only.
  2. Step 2: Check syntax and itemSize usage

    The itemSize attribute is required on cdk-virtual-scroll-viewport to set fixed item height for scrolling calculations.
  3. Final Answer:

    <cdk-virtual-scroll-viewport itemSize="50"><div *cdkVirtualFor="let item of items">{{item}}</div></cdk-virtual-scroll-viewport> -> Option C
  4. Quick Check:

    Use *cdkVirtualFor with itemSize on viewport [OK]
Hint: Use *cdkVirtualFor, not *ngFor, inside cdk-virtual-scroll-viewport [OK]
Common Mistakes:
  • Using *ngFor instead of *cdkVirtualFor
  • Using 'let item in items' instead of 'of'
  • Omitting itemSize attribute
3. Given this Angular template snippet:
<cdk-virtual-scroll-viewport itemSize="40" style="height: 120px;">
  <div *cdkVirtualFor="let item of items">{{item}}</div>
</cdk-virtual-scroll-viewport>

and items = ['A', 'B', 'C', 'D', 'E'], how many items will be visible at once?
medium
A. 3 items
B. 5 items
C. 2 items
D. 4 items

Solution

  1. Step 1: Calculate viewport height in pixels

    The viewport height is set to 120px.
  2. Step 2: Divide viewport height by itemSize

    Each item is 40px tall, so 120px / 40px = 3 items visible at once.
  3. Final Answer:

    3 items -> Option A
  4. Quick Check:

    Visible items = viewport height / itemSize = 3 [OK]
Hint: Divide viewport height by itemSize to find visible items [OK]
Common Mistakes:
  • Counting all items instead of visible ones
  • Ignoring style height on viewport
  • Using wrong division operator
4. What is wrong with this Angular virtual scroll code?
<cdk-virtual-scroll-viewport itemSize="50">
  <div *cdkVirtualFor="let item in items">{{item}}</div>
</cdk-virtual-scroll-viewport>
medium
A. The *cdkVirtualFor directive must be on the viewport, not the div.
B. Missing style height on viewport causes no scroll.
C. itemSize must be a number, not a string.
D. Using *cdkVirtualFor with 'in' instead of 'of' causes an error.

Solution

  1. Step 1: Check syntax of *cdkVirtualFor directive

    The correct syntax uses 'of' to iterate over items, not 'in'. Using 'in' causes a template error.
  2. Step 2: Verify other attributes

    itemSize can be a string representing a number, and style height is recommended but not syntax error. The directive must be on the repeated element, not viewport.
  3. Final Answer:

    Using *cdkVirtualFor with 'in' instead of 'of' causes an error. -> Option D
  4. Quick Check:

    *cdkVirtualFor uses 'of', not 'in' [OK]
Hint: Use 'of' with *cdkVirtualFor, not 'in' [OK]
Common Mistakes:
  • Using 'in' instead of 'of' in *cdkVirtualFor
  • Forgetting to set viewport height
  • Placing directive on wrong element
5. You want to display a list of 10,000 items with virtual scrolling. Which combination of settings ensures smooth scrolling and correct rendering?
hard
A. Use *ngFor inside a scrollable div without cdk-virtual-scroll-viewport.
B. Use cdk-virtual-scroll-viewport with fixed itemSize, set viewport height, and use *cdkVirtualFor.
C. Use cdk-virtual-scroll-viewport with variable itemSize and *cdkVirtualFor.
D. Use cdk-virtual-scroll-viewport without itemSize, and *ngFor for rendering.

Solution

  1. Step 1: Understand virtual scroll requirements

    Virtual scrolling requires a fixed itemSize to calculate visible items and smooth scrolling.
  2. Step 2: Use correct directives and styles

    Use cdk-virtual-scroll-viewport with a fixed height and *cdkVirtualFor to render only visible items efficiently.
  3. Step 3: Avoid common pitfalls

    Not setting itemSize or using *ngFor causes performance issues or errors.
  4. Final Answer:

    Use cdk-virtual-scroll-viewport with fixed itemSize, set viewport height, and use *cdkVirtualFor. -> Option B
  5. Quick Check:

    Fixed itemSize + *cdkVirtualFor + viewport height = smooth scroll [OK]
Hint: Fixed itemSize and *cdkVirtualFor are key for smooth virtual scroll [OK]
Common Mistakes:
  • Omitting itemSize or viewport height
  • Using *ngFor instead of *cdkVirtualFor
  • Trying variable item sizes without special handling