Skip to content

Commit

Permalink
[IMP] sheet: move cells methods performance
Browse files Browse the repository at this point in the history
The methods `moveCellOn*()` in `sheet.ts` did an useless `parseInt()` to
loop through the indexes of an array, slightly decreasing the performance.

For when we really have to parse a string to an int, Number() is also
slightly faster than parseInt().

This saves 20-40ms on a 10.000 rows sheet with 26 columns, when deleting the
first column.

Task: 3272878
Part-of: #2340
  • Loading branch information
hokolomopo committed Apr 19, 2023
1 parent 95a2949 commit 6e1334b
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions src/plugins/core/sheet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -834,10 +834,10 @@ export class SheetPlugin extends CorePlugin<SheetState> implements SheetState {
}

private moveCellOnColumnsDeletion(sheet: Sheet, deletedColumn: number) {
for (let [index, row] of Object.entries(sheet.rows)) {
const rowIndex = parseInt(index, 10);
for (let rowIndex = 0; rowIndex < sheet.rows.length; rowIndex++) {
const row = sheet.rows[rowIndex];
for (let i in row.cells) {
const colIndex = parseInt(i, 10);
const colIndex = Number(i);
const cellId = row.cells[i];
if (cellId) {
if (colIndex === deletedColumn) {
Expand Down Expand Up @@ -870,11 +870,11 @@ export class SheetPlugin extends CorePlugin<SheetState> implements SheetState {
dimension: "rows" | "columns"
) {
const commands: UpdateCellPositionCommand[] = [];
for (const [index, row] of Object.entries(sheet.rows)) {
const rowIndex = parseInt(index, 10);
for (let rowIndex = 0; rowIndex < sheet.rows.length; rowIndex++) {
const row = sheet.rows[rowIndex];
if (dimension !== "rows" || rowIndex >= addedElement) {
for (let i in row.cells) {
const colIndex = parseInt(i, 10);
const colIndex = Number(i);
const cellId = row.cells[i];
if (cellId) {
if (dimension === "rows" || colIndex >= addedElement) {
Expand Down Expand Up @@ -909,11 +909,11 @@ export class SheetPlugin extends CorePlugin<SheetState> implements SheetState {
deleteToRow: HeaderIndex
) {
const numberRows = deleteToRow - deleteFromRow + 1;
for (let [index, row] of Object.entries(sheet.rows)) {
const rowIndex = parseInt(index, 10);
for (let rowIndex = 0; rowIndex < sheet.rows.length; rowIndex++) {
const row = sheet.rows[rowIndex];
if (rowIndex >= deleteFromRow && rowIndex <= deleteToRow) {
for (let i in row.cells) {
const colIndex = parseInt(i, 10);
const colIndex = Number(i);
const cellId = row.cells[i];
if (cellId) {
this.dispatch("CLEAR_CELL", {
Expand All @@ -926,7 +926,7 @@ export class SheetPlugin extends CorePlugin<SheetState> implements SheetState {
}
if (rowIndex > deleteToRow) {
for (let i in row.cells) {
const colIndex = parseInt(i, 10);
const colIndex = Number(i);
const cellId = row.cells[i];
if (cellId) {
this.dispatch("UPDATE_CELL_POSITION", {
Expand All @@ -945,7 +945,7 @@ export class SheetPlugin extends CorePlugin<SheetState> implements SheetState {
const rows: Row[] = [];
const cellsQueue = sheet.rows.map((row) => row.cells);
for (let i in sheet.rows) {
if (parseInt(i, 10) === index) {
if (Number(i) === index) {
continue;
}
rows.push({
Expand Down

0 comments on commit 6e1334b

Please sign in to comment.