Skip to content

Commit

Permalink
perf(table): improve performance for tables without col and row span (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed Mar 14, 2022
1 parent e32f826 commit 29bcfd6
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions table/layout.ts
Expand Up @@ -50,10 +50,8 @@ export class TableLayout {
const hasHeaderBorder: boolean = this.table.hasHeaderBorder();
const hasBorder: boolean = hasHeaderBorder || hasBodyBorder;

const header: Row | undefined = this.table.getHeader();
const rows: Row<Cell>[] = this.spanRows(
header ? [header, ...this.table] : this.table.slice(),
);
const rows = this.#getRows();

const columns: number = Math.max(...rows.map((row) => row.length));
for (const row of rows) {
const length: number = row.length;
Expand Down Expand Up @@ -92,6 +90,25 @@ export class TableLayout {
};
}

#getRows() {
const header: Row | undefined = this.table.getHeader();
const rows = header ? [header, ...this.table] : this.table.slice();
const hasSpan = rows.some((row) =>
row.some((cell) =>
cell instanceof Cell && (cell.getColSpan() > 1 || cell.getRowSpan() > 1)
)
);

if (hasSpan) {
return this.spanRows(rows);
}

return rows.map((row) => {
const newRow = this.createRow(row);
return newRow.map((cell) => this.createCell(cell, newRow));
}) as Array<Row<Cell>>;
}

/**
* Fills rows and cols by specified row/col span with a reference of the
* original cell.
Expand Down

0 comments on commit 29bcfd6

Please sign in to comment.