0
0
Angularframework~10 mins

Virtual scrolling for large lists in Angular - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Virtual scrolling for large lists
User scrolls list
Calculate visible items range
Render only visible items
Update scroll position and buffer
Repeat on scroll
Virtual scrolling shows only the visible part of a large list to improve performance by rendering fewer items.
Execution Sample
Angular
import {Component} from '@angular/core';
import {CdkVirtualScrollViewport} from '@angular/cdk/scrolling';

@Component({
  selector: 'app-virtual-scroll',
  template: `<cdk-virtual-scroll-viewport itemSize="50" class="viewport">
    <div *cdkVirtualFor="let item of items" class="item">{{item}}</div>
  </cdk-virtual-scroll-viewport>`
})
export class VirtualScrollComponent {
  items = Array.from({length: 10000}).map((_, i) => `Item #${i}`);
}
This Angular component uses CDK virtual scroll to render only visible items from a 10,000-item list.
Execution Table
StepUser Scroll Position (px)Visible Range (startIndex-endIndex)Rendered Items CountAction
100-910Initial render of first 10 items
21002-1110User scrolls down, viewport updates visible range
32505-1410Viewport recalculates and renders new visible items
450010-1910Scroll continues, items updated accordingly
595019-2810Viewport shifts visible window, rendering next items
65000100-10910Fast scroll jumps to middle, renders visible items
74950009900-990910Scroll near end, renders near-last visible items
84995009990-999910Scroll reaches end, renders final items
94995509990-999910Scroll beyond end ignored, no new items rendered
💡 Scrolling stops or no new items to render beyond list end
Variable Tracker
VariableStartAfter Step 2After Step 4After Step 6After Step 8Final
scrollPositionPx01005005000499500499550
visibleStartIndex021010099909990
visibleEndIndex9111910999999999
renderedItemsCount101010101010
Key Moments - 3 Insights
Why does the component render only 10 items even though the list has 10,000?
Because virtual scrolling calculates the visible range based on scroll position and item size, rendering only items visible in the viewport (see execution_table rows 1 and 2). This improves performance by not rendering the entire list.
What happens if the user scrolls very fast down the list?
The viewport jumps to the new visible range and renders only those items (see execution_table row 6). It does not render all intermediate items, saving resources.
Why does scrolling beyond the list end not render new items?
Because the visible range is capped by the list length (see execution_table rows 8 and 9). The component prevents rendering items outside the list bounds.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4, what is the visible range of items?
A0-9
B5-14
C10-19
D19-28
💡 Hint
Check the 'Visible Range' column in execution_table row 4.
At which step does the scroll position reach 5000 pixels?
AStep 6
BStep 5
CStep 3
DStep 7
💡 Hint
Look at the 'User Scroll Position (px)' column in execution_table.
If the itemSize changes from 50 to 100 pixels, how will the rendered items count change?
ARendered items count will double
BRendered items count will halve
CRendered items count stays the same
DRendered items count becomes zero
💡 Hint
Rendered items depend on viewport height divided by itemSize; see variable_tracker for renderedItemsCount.
Concept Snapshot
Virtual scrolling shows only visible items in a large list.
Angular CDK provides cdk-virtual-scroll-viewport.
Set itemSize to define each item's height.
Viewport calculates visible range on scroll.
Only visible items are rendered, improving performance.
Scroll beyond list end does not render extra items.
Full Transcript
Virtual scrolling in Angular uses a special viewport component that shows only the items visible on screen from a large list. When the user scrolls, the component calculates which items should be visible based on the scroll position and the height of each item. It then renders only those items, not the entire list. This makes the app faster and smoother, especially with thousands of items. The example code uses Angular CDK's cdk-virtual-scroll-viewport with an item size of 50 pixels and a list of 10,000 items. The execution table shows how the visible range and rendered items update as the user scrolls down or jumps to different positions. The variable tracker follows scroll position and visible indexes. Key moments explain why only a small number of items render and how fast scrolling works. The visual quiz tests understanding of visible ranges and effects of changing item size. Overall, virtual scrolling is a smart way to handle large lists efficiently in Angular.