Evaluate copyText lazily

Summary:
`copyText` is generated when building rows for table, and is typically filled by JSON stringifying the incoming data. That is a pretty expensive process that could be done lazily. As shown in later diffs, this reduces the generation of rows for tables from ~18ms to ~3ms, which makes rendering a lot smoother.

(n.b. fixing the code duplication in the managedtables is part of the component lib plan)

Reviewed By: passy

Differential Revision: D21929660

fbshipit-source-id: 67cc69945e2bb28a6462a9d9ab765e33ced89378
This commit is contained in:
Michel Weststrate
2020-06-09 02:54:49 -07:00
committed by Facebook GitHub Bot
parent 2375dd02c3
commit 3033fb035c
3 changed files with 9 additions and 7 deletions

View File

@@ -575,9 +575,10 @@ export class ManagedTable extends React.Component<
}
return this.props.rows
.filter((row) => highlightedRows.has(row.key))
.map(
(row: TableBodyRow) =>
row.copyText || this.getTextContentOfRow(row.key).join('\t'),
.map((row: TableBodyRow) =>
typeof row.copyText === 'function'
? row.copyText()
: row.copyText || this.getTextContentOfRow(row.key).join('\t'),
)
.join('\n');
};

View File

@@ -551,9 +551,10 @@ class ManagedTable extends React.Component<
}
return this.props.rows
.filter((row) => highlightedRows.has(row.key))
.map(
(row: TableBodyRow) =>
row.copyText || this.getTextContentOfRow(row.key).join('\t'),
.map((row: TableBodyRow) =>
typeof row.copyText === 'function'
? row.copyText()
: row.copyText || this.getTextContentOfRow(row.key).join('\t'),
)
.join('\n');
};

View File

@@ -54,7 +54,7 @@ export type TableBodyRow = {
type?: string | undefined;
highlightedBackgroundColor?: BackgroundColorProperty | undefined;
onDoubleClick?: (e: React.MouseEvent) => void;
copyText?: string;
copyText?: string | (() => string);
requestBody?: string | null | undefined;
responseBody?: string | null | undefined;
highlightOnHover?: boolean;