What if your app could handle thousands of items without freezing or lagging?
Why Virtual scrolling for large lists in Vue? - Purpose & Use Cases
Imagine you have a list with thousands of items, like a long contact list or product catalog, and you try to render them all at once in your app.
Rendering every item at once makes the app slow and clunky. The browser struggles to show so many elements, causing lag and freezing. Scrolling becomes jerky and frustrating.
Virtual scrolling only renders the items visible on the screen plus a small buffer. As you scroll, it swaps items in and out smoothly, keeping the app fast and responsive.
for (let item of items) { render(item) }render(visibleItemsOnly) // update on scroll
You can smoothly display huge lists without slowing down your app or overwhelming the browser.
Think of a social media feed that loads posts as you scroll, so you never wait for all posts to load at once.
Rendering all list items at once causes slow, laggy apps.
Virtual scrolling renders only visible items for smooth performance.
This technique makes large lists user-friendly and fast.
Practice
Solution
Step 1: Understand virtual scrolling concept
Virtual scrolling renders only the visible portion of a large list instead of all items at once.Step 2: Identify the benefit in Vue apps
This reduces rendering load and improves performance when dealing with many items.Final Answer:
To render only visible items to improve performance -> Option BQuick Check:
Virtual scrolling = Render visible items only [OK]
- Thinking it loads all items at once
- Believing it disables scrolling
- Assuming it sorts items automatically
Solution
Step 1: Identify container style for virtual scrolling
The container must have a fixed height and vertical scrolling enabled (overflow-y: auto).Step 2: Match the correct style snippet
<div style="height: 300px; overflow-y: auto;"></div> sets height to 300px and overflow-y to auto, enabling vertical scroll inside fixed height.Final Answer:
<div style="height: 300px; overflow-y: auto;"></div> -> Option AQuick Check:
Fixed height + vertical scroll = <div style="height: 300px; overflow-y: auto;"></div> [OK]
- Using overflow-x instead of overflow-y
- Setting height to auto instead of fixed
- Using position fixed incorrectly
scrollTop is 200, itemHeight is 50, and visibleCount is 10?const startIndex = Math.floor(scrollTop / itemHeight); const endIndex = startIndex + visibleCount; const visibleItems = items.slice(startIndex, endIndex);
Solution
Step 1: Calculate startIndex from scrollTop and itemHeight
startIndex = floor(200 / 50) = 4.Step 2: Determine endIndex using visibleCount (assume 10)
endIndex = 4 + 10 = 14, so slice is items[4..13].Final Answer:
Items from index 4 to 13 -> Option AQuick Check:
startIndex = 4, endIndex = 14 slice = 4 to 13 [OK]
- Using scrollTop directly as index
- Confusing endIndex with startIndex
- Ignoring floor function
const startIndex = Math.ceil(scrollTop / itemHeight); const endIndex = startIndex + visibleCount; const visibleItems = items.slice(startIndex, endIndex);
Solution
Step 1: Analyze use of Math.ceil for startIndex
Math.ceil rounds up, so if scrollTop/itemHeight is exactly 4, startIndex becomes 4, but if 3.1 it becomes 4, skipping some items.Step 2: Understand impact on visible items
This causes the first partially visible item to be skipped, leading to missing items during scroll.Final Answer:
Using Math.ceil causes skipping the first visible item -> Option DQuick Check:
Math.ceil skips partially visible items [OK]
- Confusing slice with splice
- Ignoring reactive state importance
- Miscalculating endIndex
Solution
Step 1: Understand virtual scrolling requirements
Virtual scrolling needs a fixed height container with scroll enabled and dynamic rendering of visible items.Step 2: Identify correct rendering technique
Absolutely positioning items inside a tall wrapper simulates full list height while rendering only visible items improves performance.Step 3: Evaluate other options
Rendering all items or ignoring scroll position causes lag; dynamic height container breaks scroll behavior.Final Answer:
Use a fixed height container with overflow-y:auto, calculate visible items on scroll, and absolutely position items inside a tall wrapper -> Option CQuick Check:
Fixed container + dynamic visible items + absolute positioning = Use a fixed height container with overflow-y:auto, calculate visible items on scroll, and absolutely position items inside a tall wrapper [OK]
- Rendering all items causing slow UI
- Ignoring scroll position for rendering
- Using dynamic height container breaking scroll
Ready to prove it?
Pass the test and this pattern moves unknown
