Skip to content

Commit

Permalink
perf: Reduce iterative time complexity (#1060)
Browse files Browse the repository at this point in the history
- Optimize getAllSelectedFilteredItems and preSelectedRowIdsChangeFn with Set lookup and reducing time complexity from O(n square) to O(n)
  • Loading branch information
ghiscoding authored Sep 13, 2024
1 parent f47de5b commit 0d07e60
Showing 1 changed file with 4 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/slick.dataview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1471,7 +1471,8 @@ export class SlickDataView<TData extends SlickDataItem = any> implements CustomD
} else {
if (preserveHiddenOnSelectionChange && grid.getOptions().multiSelect) {
// remove rows whose id is on the list
rowIds = this.selectedRowIds!.filter((id) => args.ids.indexOf(id) === -1);
const argsIdsSet = new Set(args.ids);
rowIds = this.selectedRowIds?.filter((id) => !argsIdsSet.has(id));
} else {
rowIds = [];
}
Expand Down Expand Up @@ -1566,7 +1567,8 @@ export class SlickDataView<TData extends SlickDataItem = any> implements CustomD
return [];
}

const intersection = this.filteredItems.filter((a) => this.selectedRowIds!.some((b) => a[this.idProperty as keyof TData] === b));
const selectedRowIdSet = new Set<DataIdType>(this.selectedRowIds);
const intersection = this.filteredItems.filter((a) => selectedRowIdSet.has(a[this.idProperty as keyof TData] as DataIdType));
return (intersection || []) as T[];
}

Expand Down

0 comments on commit 0d07e60

Please sign in to comment.