qat

Virtualization

A list of 100,000 rows in the page, with nodes only for the rows in view: virtual() from @qatjs/ui/virtual (1.2 kB, opt-in; qatjs add virtual, or it comes with Table and DataGrid).

.qx20 lines, 1 reactive nameOpen in the playground
// orders.qx
import { virtual } from '@qatjs/ui/virtual'

export default function Orders({ orders }) {
  let picked = state(-1)
  const v = virtual(orders, { size: 32, overscan: 4 })
  let scroller
  mount(() => v.attach(scroller))
  return (
    <div ref={scroller} class="list" role="grid" aria-rowcount={orders.length}>
      <div style:height={v.total + 'px'} style:position="relative">
        {for (const [i, order] of entries(v))}
          <div class="row" role="row" aria-rowindex={i + 1} class:on={picked === order.id} on:click={() => (picked = order.id)}>
            {order.name} · {order.total}
          </div>
        {/for}
      </div>
    </div>
  )
}
picked state

How it draws

  • A pool of row nodes, recycled. {for} over a virtual is the compiler's own construct. Each slot of the pool is a row: its item and its index are accessors. As the list scrolls, the slot that leaves the view is given the item that enters, and only the bindings whose values changed are written. Scrolling by a row writes one row; no node is made, and nothing is reconciled.
  • A row is one element. The pool places and recycles each row by its node, so the {for}'s body is one element (or a component). An {if} goes inside it; a block or text as the row is QX-VIRTUAL-ROW, with the fix.
  • Selection is a selector. class:on={picked === order.id} in the row is 10a's selector: picking a row writes the two rows whose answer changed.
  • Fixed sizes are exact: size: 32. Measured sizes: size: 48, measure: true takes 48 as the estimate. A ResizeObserver measures each drawn row; a Fenwick tree keeps the differences (O(log n) to update and to find).
  • Scroll anchoring. A row above the view that grows or shrinks moves the scroll offset with it, so the view never jumps.
  • Horizontal: axis: 'x'. A grid is a virtual of rows and one of columns on the same scroller: a {for} over each.
  • Keyboard. v.scrollTo(i) brings row i into the window at the nearest edge; its row is then drawn, to focus (DataGrid does it for every arrow key).
  • Assistive tech. Each row says where it is in the whole: aria-rowindex={i + 1} in a grid, aria-posinset={i + 1} and aria-setsize in a list. The container carries aria-rowcount.
  • Server rendering. The first window is in the HTML: real rows, at their offsets, with the total size, so the first paint and a crawler see rows, not an empty box. The first window is view px tall (default 600): the rows that fill it, plus overscan. Resume claims those nodes; the rest virtualizes in the browser.
  • keep. A kept page that comes back has its scroll offset and its measured sizes, without measuring again.
  • Filtering 100k rows. Give it a derived in delta mode (large collections): virtual(derived(rows.filter(…), { incremental: true }), …). A row that changes costs that row, not a scan.

Table (with height), DataGrid and VirtualList are built on it.

Against TanStack Virtual

100,000 rows, the same page shell (benchmarks/virtual/): a 600 px scroller of 32 px rows, a filter, a one-row change. Qat's virtual() against React 19.2 with @tanstack/react-virtual 3.14.21 (memoized rows, a useMemo filter), in Chrome with the CPU slowed 4×. node benchmarks/virtual/bench.mjs prints this table.

Qat TanStack
first render (script) 39.6 ms 90.3 ms better
scroll: frames per second 60.5 60.7 equal (the display's rate)
scroll: script per frame 0.27 ms 0.87 ms better
scroll: script, style and layout per frame 0.54 ms 1.12 ms better
scroll: bytes allocated per frame 412 1,235 better
one-row change (the same immutable update) 8.8 ms 9.5 ms equal
one-row change, delta mode (a deep store) 0.86 ms — better
filter keystroke 12.5 ms 19.1 ms better
keyboard jump to row 50,000 0.01 ms 2.6 ms better
memory after scrolling 9.0 MB 10.4 MB better
SSR first window: rows in the HTML 23 23 equal
SSR first window: HTML (gzip) 1.2 KB 0.7 KB larger
SSR first window: time to first byte 9.8 ms 22.8 ms better

These numbers come from one run on a busy machine. The official run, on an idle machine, is pending (DECISIONS D293). Qat's server HTML is the larger because of two things a React page does not have:

  • the boot script that keeps clicks made before resume;
  • under strict CSP, a per-page stylesheet in place of style attributes (server rendering).

A keystroke in a filter whose callback reads the query reruns the callback for every row, as the chain is written. Delta mode helps the one-row change, not that.