import { useCallback, useEffect, useRef, useState } from "react"; import { VirtualList } from "./components/VirtualList"; type TabId = "virtual" | "variable" | "log"; declare global { interface Window { __setTab?: (tab: TabId) => void; __snapshot?: () => unknown; __smoke?: () => Promise; __prepareShots?: () => Promise; __pauseLog?: () => void; __mountVariable?: () => void; } } const tabs: Array<{ id: TabId; label: string }> = [ { id: "virtual", label: "虚拟 vs 全量" }, { id: "variable", label: "变高与分组" }, { id: "log", label: "日志流" }, ]; const fixedItems = Array.from({ length: 10_000 }, (_, index) => ({ id: index, text: `日志行 ${index + 1}`, })); const variableItems = Array.from({ length: 5_000 }, (_, index) => ({ id: index, text: `可变高度行 ${index + 1}`, })); function sleep(ms: number): Promise { return new Promise((resolve) => setTimeout(resolve, ms)); } function raf2(): Promise { return new Promise((resolve) => { requestAnimationFrame(() => requestAnimationFrame(() => resolve())); }); } function variableHeight(index: number): number { if (index % 100 === 0) return 36; return index % 7 === 0 ? 64 : 28; } export default function App() { const [tab, setTab] = useState("virtual"); const [virtualCount, setVirtualCount] = useState(0); const [naiveCount, setNaiveCount] = useState(0); const [virtualMs, setVirtualMs] = useState(0); const [naiveMs, setNaiveMs] = useState(0); const [virtualNodes, setVirtualNodes] = useState(0); const [naiveNodes, setNaiveNodes] = useState(0); const [firstIndex, setFirstIndex] = useState(0); const [variableCount, setVariableCount] = useState(0); const [visibleStart, setVisibleStart] = useState(0); const [logs, setLogs] = useState([]); const [paused, setPaused] = useState(false); const virtualMsRef = useRef(0); const naiveMsRef = useRef(0); const virtualRef = useRef(null); const naiveRef = useRef(null); const variableRef = useRef(null); const logRef = useRef(null); const pausedRef = useRef(false); const logSeq = useRef(1); const stickyGroup = Math.floor(visibleStart / 100) + 1; const addLogs = useCallback((count: number) => { setLogs((current) => { const next = [...current]; for (let index = 0; index < count; index += 1) { next.push(`[${String(logSeq.current).padStart(4, "0")}] 流式日志`); logSeq.current += 1; } return next; }); }, []); useEffect(() => { if (tab !== "log" || pausedRef.current || !logRef.current) return; requestAnimationFrame(() => { if (logRef.current && !pausedRef.current) { logRef.current.scrollTop = logRef.current.scrollHeight; } }); }, [logs.length, tab]); const mountVirtual = useCallback(async () => { const started = performance.now(); setVirtualCount(10_000); await raf2(); await sleep(60); const elapsed = Math.round(performance.now() - started); virtualMsRef.current = elapsed; setVirtualMs(elapsed); setVirtualNodes( document.querySelectorAll('[data-testid="virtual-row"]').length, ); }, []); const mountNaive = useCallback(async () => { const started = performance.now(); setNaiveCount(10_000); await raf2(); await sleep(80); const elapsed = Math.round(performance.now() - started); naiveMsRef.current = elapsed; setNaiveMs(elapsed); setNaiveNodes( document.querySelectorAll('[data-testid="naive-row"]').length, ); }, []); const scrollVirtual = useCallback(async (index: number) => { const list = virtualRef.current; if (!list) return; list.scrollTop = index * 36; list.dispatchEvent(new Event("scroll", { bubbles: true })); await sleep(120); const first = document.querySelector( '[data-testid="virtual-row"]', ); setFirstIndex(Number(first?.dataset.rowIndex ?? 0)); }, []); useEffect(() => { window.__setTab = (next) => setTab(next); window.__snapshot = () => ({ tab, virtualCount, virtualNodes, naiveCount, naiveNodes, firstIndex, variableCount, stickyGroup, logs: logs.length, paused, }); window.__smoke = async () => { setTab("virtual"); await sleep(50); await mountVirtual(); const nodesVirtual = document.querySelectorAll( '[data-testid="virtual-row"]', ).length; await mountNaive(); const nodesNaive = document.querySelectorAll( '[data-testid="naive-row"]', ).length; setNaiveCount(0); await scrollVirtual(5000); const first = Number( document .querySelector('[data-testid="virtual-row"]') ?.getAttribute("data-row-index") ?? 0, ); setTab("variable"); setVariableCount(5000); await sleep(150); const varList = variableRef.current; if (varList) { varList.scrollTop = 3200; varList.dispatchEvent(new Event("scroll", { bubbles: true })); } await sleep(150); const sticky = document.querySelector( '[data-testid="sticky-group"]', )?.textContent; setTab("log"); pausedRef.current = false; setPaused(false); addLogs(300); await sleep(150); if (logRef.current) { logRef.current.scrollTop = logRef.current.scrollHeight; logRef.current.dispatchEvent(new Event("scroll", { bubbles: true })); } addLogs(20); await sleep(150); const atBottom = logRef.current ? logRef.current.scrollHeight - logRef.current.scrollTop - logRef.current.clientHeight < 24 : false; if (logRef.current) { logRef.current.scrollTop = 0; logRef.current.dispatchEvent(new Event("scroll", { bubbles: true })); } await sleep(80); addLogs(5); await sleep(120); const pausedAfterScroll = pausedRef.current; pausedRef.current = false; setPaused(false); const resumed = !pausedRef.current; return { virtualNodes: nodesVirtual, naiveNodes: nodesNaive, virtualMs: virtualMsRef.current, naiveMs: naiveMsRef.current, firstIndex: first, variableTotal: 5000, sticky: sticky ?? "", atBottom, pausedAfterScroll, resumed, }; }; window.__prepareShots = async () => { setTab("virtual"); await mountVirtual(); await mountNaive(); setNaiveCount(0); await scrollVirtual(5000); }; window.__mountVariable = () => { setTab("variable"); setVariableCount(5000); }; window.__pauseLog = () => { setTab("log"); addLogs(300); pausedRef.current = true; setPaused(true); if (logRef.current) logRef.current.scrollTop = 0; }; }, [ addLogs, firstIndex, logs.length, mountNaive, mountVirtual, naiveCount, naiveNodes, paused, scrollVirtual, stickyGroup, tab, variableCount, virtualCount, virtualNodes, ]); return (

长列表工作台

虚拟化 · 变高行 · 粘性分组 · 日志跟随

{tab === "virtual" && (
虚拟 DOM 行
{virtualNodes}
虚拟耗时
{virtualMs} ms
全量 DOM 行
{naiveNodes}
全量耗时
{naiveMs} ms
{virtualCount > 0 && (
(
{item.text}
)} />

当前首行索引: {firstIndex}

)} {naiveCount > 0 && (
{Array.from({ length: naiveCount }, (_, index) => (
日志行 {index + 1}
))}
)}
)} {tab === "variable" && (
每 100 行一个分组头,第 7 行更高
{variableCount > 0 && (
分组 {stickyGroup}
setVisibleStart(start)} renderRow={(item, index) => { if (index % 100 === 0) { return (
分组 {Math.floor(index / 100) + 1}
); } return (
{item.text}
); }} />
)}
)} {tab === "log" && (
{paused ? "已暂停自动滚动" : "跟随最新"}
{ const element = logRef.current; if (!element) return; if (top === 0) { pausedRef.current = true; setPaused(true); } else if ( element.scrollHeight - top - element.clientHeight < 24 ) { pausedRef.current = false; setPaused(false); } }} renderRow={(item) => (
{item}
)} />

用户上滑即暂停;新日志继续进入列表,位置不变。

)}
); }