UI virtualization
The UI Virtualization plugin provides efficient rendering of large collections by only creating DOM elements for visible items. This dramatically improves performance when working with thousands of items by maintaining a small, consistent number of DOM elements regardless of collection size.
How It Works
Instead of creating DOM elements for every item in your collection, virtual repeat:
Calculates visible area: Determines how many items can fit in the scrollable viewport
Creates minimal views: Only renders 2x the visible items (for smooth scrolling)
Manages buffers: Uses invisible spacer elements to maintain proper scroll height
Recycles views: Reuses existing DOM elements as you scroll, updating their data context
Handles scroll events: Efficiently responds to scrolling without expensive DOM operations
Installation
Install the plugin via npm:
Register the plugin in your application:
Basic Usage
Simple List
Use virtual-repeat.for
just like the standard repeat
, with one important requirement: your container must have a fixed height and overflow: scroll
or overflow: auto
.
Unordered/Ordered Lists
Virtual repeat automatically detects list containers and handles them appropriately:
Table Virtualization
For tables, virtual repeat works on table rows while preserving the table structure:
Context Properties
Virtual repeat provides all the standard repeat context properties:
Available context properties:
$index
: Zero-based index of the current item$length
: Total number of items in the collection$first
:true
if this is the first item$last
:true
if this is the last item$middle
:true
if this is neither first nor last$even
:true
if the index is even$odd
:true
if the index is odd
Dynamic Collections
Virtual repeat efficiently handles collection mutations:
Container Requirements
Scrollable Container
Virtual repeat requires a scrollable ancestor with:
Fixed height: The container must have a defined height
Overflow scrolling:
overflow: auto
oroverflow: scroll
Item Height Requirements
Important: All items in a virtual repeat must have equal height. Virtual repeat calculates item height from the first item and applies this to all items.
Advanced Styling
CSS Classes and Conditional Styling
Use context properties for conditional styling:
Responsive Item Heights
While all items must have the same height, you can make this height responsive:
Performance Considerations
Best Practices
Keep item templates simple: Complex nested components in virtual repeat items can impact performance
Use CSS classes instead of inline styles: This reduces the work done during binding updates
Minimize watchers in item templates: Avoid complex computations in item bindings
Consider pagination for extremely large datasets: While virtual repeat handles large collections well, consider pagination for collections over 50,000 items
Memory Usage
Virtual repeat maintains only a small number of views in memory (typically 2x the visible count), making it very memory efficient:
Common Patterns
Loading States
Handle loading states in your view model:
Empty States
Provide meaningful empty states:
Filtering and Searching
Virtual repeat works seamlessly with filtered collections:
Important Limitations
Template Controllers
Virtual repeat cannot be combined with other template controllers on the same element:
Root Template Element
Virtual repeat cannot use <template>
as its root element:
CSS Pseudo-selectors
Be careful with CSS pseudo-selectors like :nth-child
as DOM elements are recycled:
Component Lifecycle
Virtual repeat recycles views, so component lifecycle methods in repeated items behave differently:
created
andattached
are called when views are first createdViews are reused as you scroll, so
binding
occurs more frequently thancreated
Use reactive patterns and change handlers instead of relying on lifecycle timing
Integration with Other Features
With Binding Behaviors
With Value Converters
Troubleshooting
Common Issues
Items not rendering correctly
Ensure your scrollable container has a fixed height
Verify that
overflow: auto
oroverflow: scroll
is setCheck that all items have equal height
Scroll position jumping
This can happen if item heights are inconsistent
Ensure all CSS that affects height is applied consistently
Performance issues
Simplify item templates
Reduce the number of bindings per item
Consider if you really need virtual repeat for smaller collections (< 100 items)
Collection updates not reflecting
Virtual repeat observes the collection properly, but ensure you're modifying the array reference if needed
For complex scenarios, manually trigger change detection
Debugging
You can access virtual repeat information programmatically:
Future Enhancements
The following features are planned for future releases:
Variable item heights: Support for items with different heights
Horizontal scrolling: Virtual repeat for horizontal layouts
Infinite scroll integration: Built-in support for loading more data
Advanced configuration: Customizable buffer sizes and scroll behavior
Performance optimizations: Even better performance for edge cases
Last updated
Was this helpful?