Skip to content

Commit 12db44e

Browse files
paulferaudandrewseguin
authored andcommitted
Avoid unneeded slice+splice in _pageData (#16555)
.slice().splice(index, pageSize) will create an unneeded full copy of the underlying data, and then also mutate it to splice a page. Neither of these operations are needed, and can be particularly slow for larger datasets. .slice(index, index + indexPageSize) is almost constant time. In case of a datasource with 1,000,000 rows (big, yes, but not absurdly so), pagination is currently visibly sluggish on my machine. Benchmarks seem to blame this: https://measurethat.net/Benchmarks/ShowResult/62222
1 parent b0937dc commit 12db44e

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

src/material/table/table-data-source.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -266,14 +266,14 @@ export class MatTableDataSource<T> extends DataSource<T> {
266266
}
267267

268268
/**
269-
* Returns a paged splice of the provided data array according to the provided MatPaginator's page
269+
* Returns a paged slice of the provided data array according to the provided MatPaginator's page
270270
* index and length. If there is no paginator provided, returns the data array as provided.
271271
*/
272272
_pageData(data: T[]): T[] {
273273
if (!this.paginator) { return data; }
274274

275275
const startIndex = this.paginator.pageIndex * this.paginator.pageSize;
276-
return data.slice().splice(startIndex, this.paginator.pageSize);
276+
return data.slice(startIndex, startIndex + this.paginator.pageSize);
277277
}
278278

279279
/**

0 commit comments

Comments
 (0)