Array, Set, generator functions, and the built-in list directives.
Arrays of TemplateResults
The simplest way to render a list is to map an array toTemplateResult values using JavaScript’s Array.prototype.map() and place the result directly in a template expression.
repeat() directive
The repeat() directive renders an iterable and performs keyed diffing — it moves, reuses, or creates DOM based on a stable key associated with each item, rather than its index.
Import: lit/directives/repeat.js
Signatures:
map() directive
map() is a lightweight alternative to Array.prototype.map() that also accepts any Iterable (not just arrays), including generators and Set.
Import: lit/directives/map.js
Signature:
Array.prototype.map(), map() accepts undefined without throwing:
range() directive
range() returns an iterable of integers, similar to Python’s range. It is useful for generating a fixed number of elements without maintaining a data array.
Import: lit/directives/range.js
Signatures:
join() directive
join() interleaves a separator value between the items of an iterable. It is useful for rendering comma-separated lists or dividers.
Import: lit/directives/join.js
Signatures:
When to use repeat() vs. map()
Both repeat() (without a key function) and map() / Array.prototype.map() update list items by index. The key difference is whether you provide a key function to repeat().
map() / Array.map()
Index-based diffing. Existing DOM nodes are reused in position order.
Fast for lists where items rarely reorder. Simpler to write.
repeat() with keyFn
Key-based diffing. DOM nodes are moved to match the new order of
keys, minimizing DOM mutations for reorders, insertions, and removals.
Use map() or Array.prototype.map() when:
- Items are read-only and never reorder.
- The list is short and performance is not a concern.
- Items do not hold internal DOM state (focused inputs, scroll position, animations).
Use repeat() with a keyFn when:
- The list can be sorted, filtered, or have items inserted/removed at arbitrary positions.
- Items hold internal DOM state that must survive reordering.
- The list is large and minimizing DOM mutations is important for performance.
Performance implications
Why keyed diffing helps for reorders
Why keyed diffing helps for reorders
Without keys, if you reverse a 100-item list, Lit updates all 100 DOM nodes
in place (changing each node’s content). With a key function,
repeat()
detects that the same DOM nodes just need to be moved, resulting in 100
insertBefore calls and zero content updates — much faster when items are
complex templates.Why index-based is sometimes faster for simple mutations
Why index-based is sometimes faster for simple mutations
Key-based reconciliation requires building key-to-index maps and scanning
the old and new lists. For appending to or truncating a list, the index-based
approach is simpler and faster because no key lookups are needed.
Memory overhead of repeat()
Memory overhead of repeat()
repeat() tracks a list of item keys between renders. For very large lists
this has a small additional memory cost compared to a plain map().