Skip to content

Commit a983e9c

Browse files
authored
perf(hooks): reduce sorting code size and data loops (#29)
1 parent f28f7f8 commit a983e9c

File tree

1 file changed

+19
-27
lines changed

1 file changed

+19
-27
lines changed

src/hooks.tsx

Lines changed: 19 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import {
1313
HeaderRenderType,
1414
ColumnStateType,
1515
} from './types';
16-
import { byTextAscending, byTextDescending } from './utils';
16+
import { byTextAscending } from './utils';
1717

1818
const createReducer = <T extends DataType>() => (
1919
state: TableState<T>,
@@ -82,7 +82,7 @@ const createReducer = <T extends DataType>() => (
8282
throw new Error(`Invalid column, ${action.columnName} not found`);
8383
}
8484

85-
let isAscending = null;
85+
let isAscending: boolean | null = null;
8686

8787
let sortedRows: RowType<T>[] = [];
8888

@@ -100,20 +100,15 @@ const createReducer = <T extends DataType>() => (
100100
column.sorted.asc === undefined ? true : !column.sorted.asc;
101101
}
102102

103-
if (column.sort) {
104-
sortedRows = isAscending
105-
? state.rows.sort(column.sort)
106-
: state.rows.sort(column.sort).reverse();
107-
// default to sort by string
108-
} else {
109-
sortedRows = isAscending
110-
? state.rows.sort(
111-
byTextAscending(object => object.original[action.columnName])
112-
)
113-
: state.rows.sort(
114-
byTextDescending(object => object.original[action.columnName])
115-
);
116-
}
103+
// default to sort by string
104+
const columnCompareFn =
105+
column.sort ||
106+
byTextAscending(object => object.original[action.columnName]);
107+
sortedRows = state.rows.sort((a, b) => {
108+
const result = columnCompareFn(a, b);
109+
return isAscending ? result : result * -1;
110+
});
111+
117112
return {
118113
...column,
119114
sorted: {
@@ -371,24 +366,21 @@ const sortByColumn = <T extends DataType>(
371366
sortColumn: string,
372367
columns: ColumnStateType<T>[]
373368
): RowType<T>[] => {
374-
let isAscending = null;
369+
let isAscending: boolean | null | undefined = null;
375370
let sortedRows: RowType<T>[] = [...data];
376371

377372
columns.map(column => {
378373
// if the row was found
379374
if (sortColumn === column.name) {
380375
isAscending = column.sorted.asc;
381376

382-
if (column.sort) {
383-
sortedRows = isAscending
384-
? data.sort(column.sort)
385-
: data.sort(column.sort).reverse();
386-
// default to sort by string
387-
} else {
388-
sortedRows = isAscending
389-
? data.sort(byTextAscending(object => object.original[sortColumn]))
390-
: data.sort(byTextDescending(object => object.original[sortColumn]));
391-
}
377+
// default to sort by string
378+
const columnCompareFn =
379+
column.sort || byTextAscending(object => object.original[sortColumn]);
380+
sortedRows = data.sort((a, b) => {
381+
const result = columnCompareFn(a, b);
382+
return isAscending ? result : result * -1;
383+
});
392384
}
393385
});
394386

0 commit comments

Comments
 (0)