🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.
TypeScript analyticsautogenevaluationlangchainlarge-language-modelsllama-indexllmllm-evaluationllm-observabilityllmopsmonitoringobservabilityopen-sourceopenaiplaygroundprompt-engineeringprompt-managementself-hostedycombinator
chore(peek): memoize table body for enhanced front-end performance (#6690)
marliessophie4e2fa5d79401
diff --git a/web/src/components/table/data-table.tsx b/web/src/components/table/data-table.tsxindex eb67f23273..c7f2f39b05 100644--- a/web/src/components/table/data-table.tsx+++ b/web/src/components/table/data-table.tsx@@ -1,6 +1,6 @@"use client";import { type OrderByState } from "@langfuse/shared";-import React, { useState, useMemo, useCallback } from "react";+import React, { useState, useMemo, useCallback, useContext } from "react";import DocPopup from "@/src/components/layouts/doc-popup";import { DataTablePagination } from "@/src/components/table/data-table-pagination";import {@@ -31,10 +31,12 @@ import {type PaginationState,type RowSelectionState,type VisibilityState,+ type Row,} from "@tanstack/react-table";import { TablePeekView } from "@/src/components/table/peek";import { type PeekViewProps } from "@/src/components/table/peek/hooks/usePeekView";import { usePeekView } from "@/src/components/table/peek/hooks/usePeekView";+import { isEqual } from "lodash";interface DataTableProps<TData, TValue> {columns: LangfuseColumnDef<TData, TValue>[];@@ -95,6 +97,8 @@ function isValidCssVariableName({return regex.test(name);}+const SelectedRowContext = React.createContext<string | undefined>(undefined);+export function DataTable<TData extends object, TValue>({columns,data,@@ -185,12 +189,16 @@ export function DataTable<TData extends object, TValue>({peekView,});- const handleOnRowClick = (row: TData) => {- if (inflatedPeekView) {- handleOnRowClickPeek?.(row);- }- onRowClick?.(row);- };+ const handleOnRowClick = useCallback(+ (row: TData) => {+ if (inflatedPeekView) {+ handleOnRowClickPeek?.(row);+ }+ onRowClick?.(row);+ },+ [handleOnRowClickPeek, onRowClick, inflatedPeekView],+ );+const hasRowClickAction = !!onRowClick || !!inflatedPeekView;// memo column sizes for performance@@ -231,136 +239,147 @@ export function DataTable<TData extends object, TValue>({className={cn("relative w-full overflow-auto border-t")}style={{ ...columnSizeVars }}>- <Table>- <TableHeader className="sticky top-0 z-10">- {tableHeaders.map((headerGroup) => (- <TableRow key={headerGroup.id}>- {headerGroup.headers.map((header) => {- const columnDef = header.column- .columnDef as LangfuseColumnDef<ModelTableRow>;- const sortingEnabled = columnDef.enableSorting;- // if the header id does not translate to a valid css variable name, default to 150px as width- // may only happen for dynamic columns, as column names are user defined- const width = isValidCssVariableName({- name: header.id,- includesHyphens: false,- })- ? `calc(var(--header-${header.id}-size) * 1px)`- : 150;+ <SelectedRowContext.Provider value={peekViewId}>+ <Table>+ <TableHeader className="sticky top-0 z-10">+ {tableHeaders.map((headerGroup) => (+ <TableRow key={headerGroup.id}>+ {headerGroup.headers.map((header) => {+ const columnDef = header.column+ .columnDef as LangfuseColumnDef<ModelTableRow>;+ const sortingEnabled = columnDef.enableSorting;+ // if the header id does not translate to a valid css variable name, default to 150px as width+ // may only happen for dynamic columns, as column names are user defined+ const width = isValidCssVariableName({+ name: header.id,+ includesHyphens: false,+ })+ ? `calc(var(--header-${header.id}-size) * 1px)`+ : 150;- return header.column.getIsVisible() ? (- <TableHead- key={header.id}- className={cn(- "group p-1 first:pl-2",- sortingEnabled && "cursor-pointer",- pinFirstColumn &&- header.index === 0 &&- "sticky left-0 z-20 border-r bg-background",- )}- style={{ width }}- onClick={(event) => {- event.preventDefault();+ return header.column.getIsVisible() ? (+ <TableHead+ key={header.id}+ className={cn(+ "group p-1 first:pl-2",+ sortingEnabled && "cursor-pointer",+ pinFirstColumn &&+ header.index === 0 &&+ "sticky left-0 z-20 border-r bg-background",+ )}+ style={{ width }}+ onClick={(event) => {+ event.preventDefault();- if (!setOrderBy || !columnDef.id || !sortingEnabled) {- return;- }+ if (+ !setOrderBy ||+ !columnDef.id ||+ !sortingEnabled+ ) {+ return;+ }- if (orderBy?.column === columnDef.id) {- if (orderBy.order === "DESC") {+ if (orderBy?.column === columnDef.id) {+ if (orderBy.order === "DESC") {+ capture("table:column_sorting_header_click", {+ column: columnDef.id,+ order: "ASC",+ });+ setOrderBy({+ column: columnDef.id,+ order: "ASC",+ });+ } else {+ capture("table:column_sorting_header_click", {+ column: columnDef.id,+ order: "Disabled",+ });+ setOrderBy(null);+ }+ } else {capture("table:column_sorting_header_click", {column: columnDef.id,- order: "ASC",+ order: "DESC",});setOrderBy({column: columnDef.id,- order: "ASC",- });- } else {- capture("table:column_sorting_header_click", {- column: columnDef.id,- order: "Disabled",+ order: "DESC",});- setOrderBy(null);}- } else {- capture("table:column_sorting_header_click", {- column: columnDef.id,- order: "DESC",- });- setOrderBy({- column: columnDef.id,- order: "DESC",- });- }- }}- >- {header.isPlaceholder ? null : (- <div className="flex select-none items-center">- <span className="truncate">- {flexRender(- header.column.columnDef.header,- header.getContext(),+ }}+ >+ {header.isPlaceholder ? null : (+ <div className="flex select-none items-center">+ <span className="truncate">+ {flexRender(+ header.column.columnDef.header,+ header.getContext(),+ )}+ </span>+ {columnDef.headerTooltip && (+ <DocPopup+ description={+ columnDef.headerTooltip.description+ }+ href={columnDef.headerTooltip.href}+ />)}- </span>- {columnDef.headerTooltip && (- <DocPopup- description={- columnDef.headerTooltip.description- }- href={columnDef.headerTooltip.href}- />- )}- {orderBy?.column === columnDef.id- ? renderOrderingIndicator(orderBy)- : null}+ {orderBy?.column === columnDef.id+ ? renderOrderingIndicator(orderBy)+ : null}- <div- onClick={(e) => {- e.preventDefault();- e.stopPropagation();- }}- onDoubleClick={() => header.column.resetSize()}- onMouseDown={header.getResizeHandler()}- onTouchStart={header.getResizeHandler()}- className={cn(- "absolute right-0 top-0 h-full w-1.5 cursor-col-resize touch-none select-none bg-secondary opacity-0 group-hover:opacity-100",- header.column.getIsResizing() &&- "bg-primary-accent opacity-100",- )}- />- </div>- )}- </TableHead>- ) : null;- })}- </TableRow>- ))}- </TableHeader>- {table.getState().columnSizingInfo.isResizingColumn ? (- <MemoizedTableBody- table={table}- rowheighttw={rowheighttw}- columns={columns}- data={data}- help={help}- onRowClick={hasRowClickAction ? handleOnRowClick : undefined}- peekViewId={peekViewId}- pinFirstColumn={pinFirstColumn}- />- ) : (- <TableBodyComponent- table={table}- rowheighttw={rowheighttw}- columns={columns}- data={data}- help={help}- onRowClick={hasRowClickAction ? handleOnRowClick : undefined}- peekViewId={peekViewId}- pinFirstColumn={pinFirstColumn}- />- )}- </Table>+ <div+ onClick={(e) => {+ e.preventDefault();+ e.stopPropagation();+ }}+ onDoubleClick={() => header.column.resetSize()}+ onMouseDown={header.getResizeHandler()}+ onTouchStart={header.getResizeHandler()}+ className={cn(+ "absolute right-0 top-0 h-full w-1.5 cursor-col-resize touch-none select-none bg-secondary opacity-0 group-hover:opacity-100",+ header.column.getIsResizing() &&+ "bg-primary-accent opacity-100",+ )}+ />+ </div>+ )}+ </TableHead>+ ) : null;+ })}+ </TableRow>+ ))}+ </TableHeader>+ {table.getState().columnSizingInfo.isResizingColumn ||+ !!peekView ? (+ <MemoizedTableBody+ table={table}+ rowheighttw={rowheighttw}+ columns={columns}+ data={data}+ help={help}+ onRowClick={hasRowClickAction ? handleOnRowClick : undefined}+ pinFirstColumn={pinFirstColumn}+ tableSnapshot={{+ tableDataUpdatedAt: peekView?.tableDataUpdatedAt,+ columnVisibility,+ columnOrder,+ rowSelection,+ }}+ />+ ) : (+ <TableBodyComponent+ table={table}+ rowheighttw={rowheighttw}+ columns={columns}+ data={data}+ help={help}+ onRowClick={hasRowClickAction ? handleOnRowClick : undefined}+ pinFirstColumn={pinFirstColumn}+ />+ )}+ </Table>+ </SelectedRowContext.Provider></div><div className="grow"></div></div>@@ -400,8 +419,43 @@ interface TableBodyComponentProps<TData> {data: AsyncTableData<TData[]>;help?: { description: string; href: string };onRowClick?: (row: TData) => void;- peekViewId?: string;pinFirstColumn?: boolean;+ tableSnapshot?: {+ tableDataUpdatedAt?: number;+ columnVisibility?: VisibilityState;+ columnOrder?: ColumnOrderState;+ rowSelection?: RowSelectionState;+ };+}++function TableRowComponent<TData>({+ row,+ onRowClick,+ children,+}: {+ row: Row<TData>;+ onRowClick?: (row: TData) => void;+ children: React.ReactNode;+}) {+ const peekViewId = useContext(SelectedRowContext);+ return (+ <TableRow+ data-row-index={row.index}+ onClick={() => onRowClick?.(row.original)}+ onKeyDown={(e) => {+ if (e.key === "Enter") {+ onRowClick?.(row.original);+ }+ }}+ className={cn(+ "hover:bg-accent",+ !!onRowClick ? "cursor-pointer" : "cursor-default",+ peekViewId && peekViewId === row.id ? "bg-accent" : undefined,+ )}+ >+ {children}+ </TableRow>+ );}function TableBodyComponent<TData>({@@ -411,7 +465,6 @@ function TableBodyComponent<TData>({data,help,onRowClick,- peekViewId,pinFirstColumn = false,}: TableBodyComponentProps<TData>) {return (@@ -427,21 +480,7 @@ function TableBodyComponent<TData>({</TableRow>) : table.getRowModel().rows.length ? (table.getRowModel().rows.map((row) => (- <TableRow- key={row.id}- data-row-index={row.index}- onClick={() => onRowClick?.(row.original)}- onKeyDown={(e) => {- if (e.key === "Enter") {- onRowClick?.(row.original);- }- }}- className={cn(- "hover:bg-accent",- !!onRowClick ? "cursor-pointer" : "cursor-default",- peekViewId && peekViewId === row.id ? "bg-accent" : undefined,- )}- >+ <TableRowComponent key={row.id} row={row} onRowClick={onRowClick}>{row.getVisibleCells().map((cell) => (<TableCellkey={cell.id}@@ -461,7 +500,7 @@ function TableBodyComponent<TData>({</div></TableCell>))}- </TableRow>+ </TableRowComponent>))) : (<TableRow className="hover:bg-transparent">@@ -479,8 +518,51 @@ function TableBodyComponent<TData>({);}-// memo tables for performance, should only re-render when data changes-// https://tanstack.com/table/v8/docs/guide/column-sizing#advanced-column-resizing-performance+// Optimize table rendering performance by memoizing the table body+// This is critical for two high-frequency re-render scenarios:+// 1. During column resizing: When users drag column headers, it can trigger+// many state updates that would otherwise cause the entire table to re-render.+// 2. When using peek views: URL/state changes from peek view navigation would+// otherwise cause unnecessary table re-renders.+//+// We need to ensure the table re-renders when:+// - The actual data changes (including metrics loaded asynchronously and pagination state)+// - The loading state changes+// - The new column widths are computed+// - The row height changes+// - The number of visible cells changes+// - The column order changes+//+// See: https://tanstack.com/table/v8/docs/guide/column-sizing#advanced-column-resizing-performanceconst MemoizedTableBody = React.memo(TableBodyComponent, (prev, next) => {- return prev.table.options.data === next.table.options.data;+ if (!prev.tableSnapshot || !next.tableSnapshot)+ return !prev.tableSnapshot && !next.tableSnapshot;++ // Check reference equality first (faster)+ if (+ prev.tableSnapshot.tableDataUpdatedAt !==+ next.tableSnapshot.tableDataUpdatedAt+ )+ return false;+ if (prev.table.options.data !== next.table.options.data) return false;+ if (prev.data.isLoading !== next.data.isLoading) return false;+ if (prev.rowheighttw !== next.rowheighttw) return false;++ // Then do more expensive deep equality checks+ if (+ !isEqual(prev.tableSnapshot.rowSelection, next.tableSnapshot.rowSelection)+ )+ return false;+ if (+ !isEqual(+ prev.tableSnapshot.columnVisibility,+ next.tableSnapshot.columnVisibility,+ )+ )+ return false;+ if (!isEqual(prev.tableSnapshot.columnOrder, next.tableSnapshot.columnOrder))+ return false;++ // If all checks pass, components are equal+ return true;}) as typeof TableBodyComponent;diff --git a/web/src/components/table/peek/hooks/usePeekView.ts b/web/src/components/table/peek/hooks/usePeekView.tsindex 894eaf155d..fc8c79a8ea 100644--- a/web/src/components/table/peek/hooks/usePeekView.ts+++ b/web/src/components/table/peek/hooks/usePeekView.ts@@ -2,10 +2,27 @@ import { type DataTablePeekViewProps } from "@/src/components/table/peek";import { useRouter } from "next/router";import { useEffect, useRef, useState } from "react";+/**+ * Props for configuring a peek view in a data table.+ *+ * @template TData The type of data in the table rows+ *+ * @property isTableDataComplete - Indicates whether all data (including asynchronously loaded data like metrics)+ * has finished loading. This is critical for proper table memoization - when false, it ensures the table+ * continues to re-render as additional data loads, even when the primary data reference remains stable.+ * Set this to true only when ALL data needed for rendering the table is fully loaded.+ *+ * Common usage pattern:+ * ```+ * isTableDataComplete: !metricsQuery.isLoading && metricsQuery.data !== undefined+ * ```+ */export type PeekViewProps<TData> = Omit<DataTablePeekViewProps<TData>,"selectedRowId" | "row"->;+> & {+ tableDataUpdatedAt: number;+};function getInitialRow<TData>(peekViewId: string | undefined,diff --git a/web/src/components/table/use-cases/observations.tsx b/web/src/components/table/use-cases/observations.tsxindex 8629ea6863..b59f35cf09 100644--- a/web/src/components/table/use-cases/observations.tsx+++ b/web/src/components/table/use-cases/observations.tsx@@ -945,6 +945,7 @@ export default function ObservationsTable({children: (row) => (<PeekViewObservationDetail projectId={projectId} row={row} />),+ tableDataUpdatedAt: generations.dataUpdatedAt,}}data={generations.isLoadingdiff --git a/web/src/components/table/use-cases/traces.tsx b/web/src/components/table/use-cases/traces.tsxindex df184f31a6..7ba3996af3 100644--- a/web/src/components/table/use-cases/traces.tsx+++ b/web/src/components/table/use-cases/traces.tsx@@ -1099,6 +1099,10 @@ export default function TracesTable({onExpand: expandPeek,getNavigationPath,children: <PeekViewTraceDetail projectId={projectId} />,+ tableDataUpdatedAt: Math.max(+ traces.dataUpdatedAt,+ traceMetrics.dataUpdatedAt,+ ),}}/></>diff --git a/web/src/components/ui/sheet.tsx b/web/src/components/ui/sheet.tsxindex d22982efee..97507ae38c 100644--- a/web/src/components/ui/sheet.tsx+++ b/web/src/components/ui/sheet.tsx@@ -31,7 +31,7 @@ const SheetOverlay = React.forwardRef<SheetOverlay.displayName = SheetPrimitive.Overlay.displayName;const sheetVariants = cva(- "fixed z-50 gap-4 bg-background p-6 shadow-lg transition ease-in-out data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:duration-150 data-[state=open]:duration-200",+ "fixed z-50 gap-4 bg-background p-6 shadow-lg transition ease-in-out data-[state=open]:animate-in data-[state=closed]:animate-out data-[state=closed]:duration-100 data-[state=open]:duration-100",{variants: {side: {diff --git a/web/src/features/datasets/components/DatasetCompareRunsTable.tsx b/web/src/features/datasets/components/DatasetCompareRunsTable.tsxindex 25ea867a4c..006023764c 100644--- a/web/src/features/datasets/components/DatasetCompareRunsTable.tsx+++ b/web/src/features/datasets/components/DatasetCompareRunsTable.tsx@@ -468,6 +468,10 @@ export function DatasetCompareRunsTable(props: {peekView={{itemType: "DATASET_ITEM",urlPathname,+ tableDataUpdatedAt: Math.max(+ baseDatasetItems.dataUpdatedAt,+ ...runs.map(({ items }) => items.dataUpdatedAt),+ ),onOpenChange: setPeekView,getNavigationPath,shouldUpdateRowOnDetailPageNavigation,