-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
perf(DataTable): speed up select-all with large tables #13831
perf(DataTable): speed up select-all with large tables #13831
Conversation
DCO Assistant Lite bot All contributors have signed the DCO. |
✅ Deploy Preview for carbon-components-react ready!Built without sensitive environment variables
To edit notification comments on pull requests, go to your Netlify site settings. |
✅ Deploy Preview for carbon-elements ready!
To edit notification comments on pull requests, go to your Netlify site settings. |
d77e1c1
to
a5b3d71
Compare
I have read the DCO document and I hereby sign the DCO. |
Rewrite `setAllSelectedState` to improve peformance with large tables. In my test case with 10000 rows, selecting all rows previously took 14000ms. This fix reduces it to 8ms. It uses one instance of (local) variable mutation. Fixes carbon-design-system#6146. .
a5b3d71
to
8a6f374
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mrspeaker Thank you, this is great! 🎉 This will also close #4338, I've already updated the issue body to reflect this.
Do you think we could speed it up even more by not relying on reduce
and just using a for loop instead to build up the rowsByID?
@tay1orjones - strangely, I thought the same - but when I tested it the |
@mrspeaker Very interesting, thanks for testing it out! |
Rewrite
setAllSelectedState
to improve performance with large tables. In my test case on my machine with 10000 rows, selecting all rows previously took 14000ms, this fix reduces it to 8ms. It uses one instance of (local) variable mutation.Closes #6146
Closes #4338
The
setAllSelectedState
function is called when you click the select-all (or deselect all), or select one row then hit the cancel button on the bulk-action toolbar (inhandleOnCancel
). It appears to be "accidentally quadratic" and starts to become an issue with more than a few thousand rows. To avoid it, I do a local mutation of the accumulator rather than re-splatting out the entire object each iteration.For 10000 rows, this reduces the original 14 second time (this is for Vite transpiled code, 8 seconds for native), down to ~250ms. Most of that time is in the
filteredRowIds.includes
check. So I added an extra flag (isFiltered
) to avoid calling when the filter is not in use. When there are no filtered rows, the function runs in ~8ms.Changelog
Testing / Reviewing
The fix does not break any existing tests, and should not introduce any changes to the existing behavior of the DataTable: Selecting-all/deselecting all should work as expected. When using the bulk-action toolbar, clicking cancel should deselect the row. If rows are filtered, clicking select-all should select all filtered rows.
To test the performance, you need to make a table with a lot of (at least 10000) rows. With a large dataset like this, the performance improvements are obvious, but if you want to measure it then wrap calls to
setAllSelectedState
with console.time messages. E.g, inside the DataTable'shandleOnCancel
function: