Skip to content

Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.

[fix] Improve render cell size script perf for data table rendering (#2104)

Igor Dykhta0cd0e3792062
diff --git a/src/components/src/common/data-table/cell-size.ts b/src/components/src/common/data-table/cell-size.ts
index 040f659c6..eb049d3ab 100644
--- a/src/components/src/common/data-table/cell-size.ts
+++ b/src/components/src/common/data-table/cell-size.ts
@@ -70,6 +70,7 @@ export function renderedSize({
header: 0
};
}
+
const textCanvas = document.createElement('canvas');
document.body.appendChild(textCanvas);
const context = textCanvas.getContext('2d');
@@ -84,12 +85,16 @@ export function renderedSize({
rowsToSample = Array.from(Array(dataContainer.numRows()).keys());
}
const rowWidth = Math.max(
- ...rowsToSample.map(
- rowIdx =>
- Math.ceil(
- context.measureText(parseFieldValue(dataContainer.valueAt(rowIdx, colIdx), type)).width
- ) + cellPadding
- )
+ ...rowsToSample.map(rowIdx => {
+ const value = parseFieldValue(dataContainer.valueAt(rowIdx, colIdx), type);
+ // measuring large text cause slow performance
+ if (value.length > maxCellSize) {
+ return maxCellSize;
+ } else {
+ const textWidth = context.measureText(value).width;
+ return Math.ceil(textWidth) + cellPadding;
+ }
+ })
);
// header cell only has left padding
const headerWidth =