Skip to content

An open source design system that's fully customizable and agent ready

TypeScript

perf(Table): a re-sort moves rows without re-rendering them in keyed tables with no row indexing or body-row plugin (#9234)

Cindy Zhang2babd6693bcc
diff --git a/.changeset/table-resort-keeps-rows.md b/.changeset/table-resort-keeps-rows.md
new file mode 100644
index 0000000000..6409e05293
--- /dev/null
+++ b/.changeset/table-resort-keeps-rows.md
@@ -0,0 +1,7 @@
+---
+'@astryxdesign/core': patch
+---
+
+[perf] Table: when nothing in a table depends on a row's position, re-sorting it no longer re-renders every row; the rows only move. On a 1,000-row table this is about a tenth of the work a sort click does. This applies to a table that has an `idKey`, sets neither `rowCount` nor `rowIndexStart`, and uses neither `useTableRowIndex` nor a plugin that transforms body rows (selection, row expansion, grouped rows, tree data). Those tables still re-render their rows on a sort.
+
+@cixzhang
diff --git a/packages/core/src/Table/BaseTable.tsx b/packages/core/src/Table/BaseTable.tsx
index d57896b377..f25939f60c 100644
--- a/packages/core/src/Table/BaseTable.tsx
+++ b/packages/core/src/Table/BaseTable.tsx
@@ -266,10 +266,10 @@ function TableRowInner<T extends Record<string, unknown>>({
* Compares TableRowProps to determine if re-render is needed.
* Shallow compares the item object and checks if columns/plugins references changed.
*
- * Includes rowIndex in the comparison so that plugins using the index
- * (e.g. for row numbering or conditional formatting) get correct values
- * after insertions/deletions. This also future-proofs for tree grid
- * index paths where structural position matters.
+ * The row index reaches a row's output only through a plugin's
+ * `transformBodyRow` (cells get no index), so it is compared only when some
+ * plugin has one: a re-sort that moves rows then re-renders none of them
+ * unless a plugin can show the index (row numbering, conditional formatting).
*/
function areRowPropsEqual<T extends Record<string, unknown>>(
prevProps: TableRowProps<T>,
@@ -278,7 +278,10 @@ function areRowPropsEqual<T extends Record<string, unknown>>(
if (prevProps.rowKey !== nextProps.rowKey) {
return false;
}
- if (prevProps.rowIndex !== nextProps.rowIndex) {
+ if (
+ prevProps.rowIndex !== nextProps.rowIndex &&
+ nextProps.plugins.some(plugin => plugin.transformBodyRow != null)
+ ) {
return false;
}
if (prevProps.ariaRowIndex !== nextProps.ariaRowIndex) {
diff --git a/packages/core/src/Table/Table.perf.test.tsx b/packages/core/src/Table/Table.perf.test.tsx
index f6d0c13097..966641f57d 100644
--- a/packages/core/src/Table/Table.perf.test.tsx
+++ b/packages/core/src/Table/Table.perf.test.tsx
@@ -17,8 +17,9 @@ import {render, screen, act} from '@testing-library/react';
import {useMemo, useState} from 'react';
import {Table} from './Table';
import {useTableRowStatus} from './plugins/rowStatus';
+import {useTableRowIndex} from './plugins/rowIndex';
import type {UseTableRowStatusConfig} from './index';
-import type {TableColumn} from './types';
+import type {TableColumn, TablePlugin} from './types';
// =============================================================================
// Test Data
@@ -567,3 +568,92 @@ describe('Table render performance', () => {
});
});
});
+
+// =============================================================================
+// Test: a re-sort moves rows without re-rendering them
+// =============================================================================
+
+describe('re-sort', () => {
+ const sortedRows = createTestData(1000);
+ const reversedRows = [...sortedRows].reverse();
+
+ function renderSortable(
+ renderCell: (row: TestRow) => string,
+ plugins?: Record<string, TablePlugin<TestRow>>,
+ ) {
+ const columns: TableColumn<TestRow>[] = [
+ {key: 'name', header: 'Name', renderCell},
+ {key: 'value', header: 'Value'},
+ ];
+ const view = render(
+ <Table
+ data={sortedRows}
+ columns={columns}
+ idKey="id"
+ plugins={plugins}
+ />,
+ );
+ return () =>
+ view.rerender(
+ <Table
+ data={reversedRows}
+ columns={columns}
+ idKey="id"
+ plugins={plugins}
+ />,
+ );
+ }
+
+ it('re-renders no cell of 1,000 rows when no plugin reads the row index', () => {
+ const renderCell = vi.fn((row: TestRow) => row.name);
+ const resort = renderSortable(renderCell);
+ renderCell.mockClear();
+
+ resort();
+
+ expect(renderCell).not.toHaveBeenCalled();
+ expect(screen.getAllByRole('row')[1]).toHaveTextContent('Item 999');
+ });
+
+ it('re-renders the rows when a plugin reads the row index', () => {
+ const renderCell = vi.fn((row: TestRow) => row.name);
+ const stripe: TablePlugin<TestRow> = {
+ transformBodyRow: (props, _item, index) => ({
+ ...props,
+ htmlProps: {...props.htmlProps, 'data-odd': index % 2 === 1},
+ }),
+ };
+ const resort = renderSortable(renderCell, {stripe});
+ renderCell.mockClear();
+
+ resort();
+
+ expect(renderCell).toHaveBeenCalledTimes(1000);
+ const [, first, second] = screen.getAllByRole('row');
+ expect(first).toHaveTextContent('Item 999');
+ expect(first).toHaveAttribute('data-odd', 'false');
+ expect(second).toHaveAttribute('data-odd', 'true');
+ });
+
+ it('keeps row numbers in step with the new order', () => {
+ function Numbered({data}: {data: TestRow[]}) {
+ const rowIndex = useTableRowIndex({data});
+ return (
+ <Table
+ data={data}
+ columns={testColumns}
+ idKey="id"
+ plugins={{rowIndex}}
+ />
+ );
+ }
+ const small = createTestData(3);
+ const view = render(<Numbered data={small} />);
+
+ view.rerender(<Numbered data={[...small].reverse()} />);
+
+ const [, first, , last] = screen.getAllByRole('row');
+ expect(first).toHaveTextContent(/^1row-2/);
+ expect(last).toHaveTextContent(/^3row-0/);
+ });
+});