How does the Angular CDK's cdk-virtual-scroll-viewport show a 100,000-line audit log without creating 100,000 DOM rows?
answer
- a box that fakes its full length
- an empty element sized for everything
- item count times row height
- rendered range plus a pixel buffer
- translateY on the content wrapper
basics
~20 sThe viewport sizes an empty spacer to the whole list's height so the scrollbar looks right, then renders only the rows in view plus a small buffer, shifting them into place with a CSS transform.
solid answer
~40 s`<cdk-virtual-scroll-viewport>` from `@angular/cdk/scrolling` is its own scrolling box. A scroll strategy — with `itemSize="32"`, the built-in fixed-size one — multiplies row height by row count and gives the total to an empty spacer element, so 100,000 lines of 32px produce a 3,200,000px scroll range with no rows behind it. On each animation frame after a scroll, the strategy works out which indexes are visible, asks `*cdkVirtualFor` to render just that range plus a pixel buffer above and below, and moves the wrapper holding those rows with `translateY` to the right offset. Rows leaving the range are detached and, up to a cache limit, kept for reuse. The DOM holds a few dozen rows whatever the list length, so layout, style work and memory stay flat.
code
html · 5 lines<cdk-virtual-scroll-viewport itemSize="32" class="audit-log">
<div class="audit-line" *cdkVirtualFor="let line of lines(); let i = index">
{{ i + 1 }} · {{ line.timestamp }} · {{ line.actor }} · {{ line.action }}
</div>
</cdk-virtual-scroll-viewport>go deeper
Recall the trick in one breath: a spacer sized for the whole list makes the scrollbar honest, and only the visible rows plus a buffer are real elements, moved into place with a transform.
Explain who computes what: the strategy injected through VIRTUAL_SCROLL_STRATEGY sets the total size and the rendered range, *cdkVirtualFor turns that range into views, and the template cache recycles detached ones.
Show you know the price: no find-in-page, no DOM queries for off-screen rows, a fixed height on the box and a row-height contract, and say how you would compensate in an audit viewer.
Frame when the viewport is worth it at all: thousands of uniform rows justify it, a few hundred rich rows may not, and the decision belongs with the product's need for search and in-page navigation.
## What the viewport is `<cdk-virtual-scroll-viewport>` is a component in the Angular CDK's `@angular/cdk/scrolling` entry point. It is a scrolling container that **virtualizes** its content: to the user and to the scrollbar the list behaves as if every item were present, yet only a small window of items is actually rendered. Paired with the `*cdkVirtualFor` structural directive in place of a plain loop, it is the CDK's way to put a 100,000-line security audit log in one scrollable box. The CDK guide contrasts this with **infinite scroll**: infinite scroll renders a batch, appends the next batch when you reach the end and keeps everything it appended, while the viewport keeps the DOM small at every scroll position. ## The moving parts 1. **The viewport element** — the box that scrolls. By default it is itself the scroll container: it gets the class `cdk-virtual-scrollable`, styled with `overflow: auto` and `contain: strict`, so it needs an explicit height. 2. **A scroll strategy** — an object implementing the CDK's `VirtualScrollStrategy` interface, injected through the `VIRTUAL_SCROLL_STRATEGY` token. Adding `itemSize="32"` to the viewport attaches the built-in **fixed-size strategy**, which assumes every row is 32px tall. 3. **The spacer** — an empty element (`cdk-virtual-scroll-spacer`) whose height the strategy sets to the **total content size**. For the fixed-size strategy that is item count × `itemSize`: 100,000 × 32px = 3,200,000px. The spacer is what makes the scrollbar thumb small and draggable across the whole log. 4. **The content wrapper** — an absolutely positioned element (`cdk-virtual-scroll-content-wrapper`) holding the rows that are actually rendered. The viewport moves it with a `translateY(...)` transform to the offset where those rows belong. 5. **`*cdkVirtualFor`** — the repeater that turns the current rendered range into embedded views. It offers the familiar loop context variables (`index`, `count`, `first`, `last`, `even`, `odd`), all relative to the **full data set**, not to the rendered slice. ## What happens when the user scrolls 1. Scroll events from the scroll container are batched so the layout is rechecked at most once per animation frame. 2. The strategy reads the scroll offset and the viewport's size and computes the first visible index — with fixed sizes, simply the offset divided by `itemSize`. 3. It decides a **rendered range**: the visible rows plus a **buffer** of extra pixels before and after, governed by `minBufferPx` (default 100) and `maxBufferPx` (default 200). 4. The viewport publishes the new range; `*cdkVirtualFor` diffs the slice of data in that range against what is on screen, adds views for rows entering and detaches views for rows leaving. 5. The wrapper's transform becomes the range start × `itemSize`, so the rendered rows sit exactly where the full list would have put them. Detached views are not necessarily destroyed. `*cdkVirtualFor` keeps up to **20** of them in a **template cache** and hands them to the next rows that need a view, which saves creating DOM nodes and component instances on every scroll step. ## What is in memory and in the DOM | Thing | For 100,000 lines of 32px in a 480px-tall viewport | |---|---| | Data array | 100,000 entries, all in memory | | Spacer height | 3,200,000px | | Visible rows | about 15 | | Rendered rows | the visible 15 plus a few buffer rows on each side | | Detached, cached views | up to 20 | Style recalculation, layout and per-row memory scale with the rendered rows rather than with the data length, so the log scrolls the same at 1,000 lines or 100,000. With an array as the source, the data itself is still fully loaded; the viewport saves DOM, not data. ## Limits to know from day one - **A strategy is mandatory.** Without `itemSize` or another strategy provider, the viewport throws in development mode with an error saying it requires the `itemSize` property to be set. - **The viewport needs a height.** Because it is a strictly contained scroll box, a viewport whose height is left to its content collapses to zero and shows nothing. - **Off-screen rows do not exist.** The browser's find-in-page cannot find an audit line that is not rendered, and code that queries the DOM for line 70,000 finds nothing. Work from the data array and the viewport's own methods, such as `scrollToIndex`, instead. - **Row height is a contract.** The fixed-size strategy trusts `itemSize`; rows taller or shorter than it make positions drift, and lists whose rows genuinely differ in height need a different strategy. - **It replaces the loop, not the list markup.** For elements with parent requirements such as `<li>` or `<tr>`, put the proper parent inside the viewport and repeat the children with `*cdkVirtualFor`; the viewport's styles reset that parent's top and bottom margin, padding and border so they do not throw off the arithmetic. The trade, in short: the viewport gives up "every row is in the document" in exchange for a DOM whose size does not grow with the data.
- Is the CDK viewport the same thing as infinite scroll?No. Infinite scroll renders a batch, appends more when you reach the end and keeps everything it appended, so the DOM grows with how far the user scrolled. The viewport renders only the visible range plus a buffer at every position and removes rows that leave it. The two combine well: load pages of audit entries as the user nears the end, and let the viewport keep the DOM small.
- Why does the viewport move a wrapper with translateY instead of giving each row an absolute top?Moving one wrapper is simpler and cheaper than positioning each row: the rendered rows stay in normal flow inside the wrapper, and only the wrapper's offset changes when the range shifts. The spacer beside it supplies the full scroll height, so the scrollbar reflects all 100,000 lines while the wrapper holds a few dozen.
- Does virtual scrolling reduce how much data the page loads?Not by itself. `*cdkVirtualFor` accepts an array, an Observable of an array or a DataSource, and with an array the whole log is in memory; the viewport saves DOM nodes, layout and change-detection work. To avoid loading 100,000 entries, feed it a DataSource that fetches pages as ranges are requested.
- Virtual scrolling hides off-screen rows with CSS but keeps them in the DOM.
- The CDK viewport is just infinite scroll that appends rows at the end.
- The viewport measures every row to size its scrollbar.
- A virtual scroll viewport works without a height because it grows with content.
- Browser find-in-page still finds lines that have scrolled out of the viewport.
- Virtual scrolling means the data array no longer needs to be in memory.