Repository navigation
perf: improve useSet operations performance - #2742
Open
DamianKocjan wants to merge 1 commit into
Open
DamianKocjan wants to merge 1 commit into
DamianKocjan wants to merge 1 commit into
Conversation
Instead of converting set objects to array and using array methods (O(n)), we utilize direct set methods (O(1))
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Instead of converting set objects to array and using array methods (O(n)), we utilize direct set methods (O(1))
Description
Optimized
useSetoperations by replacing intermediate array conversions (O(n)) with nativeSetmethods (O(1)) and avoiding unnecessary re-renders:has()(O(1)) to bail out early if the item already exists; otherwise adds vianewSet.add()instead of array spread.has()(O(1)) to bail out early if the item is missing; otherwise deletes vianewSet.delete()instead ofarray .filter().has(),delete(), andadd()directly on the cloned Set in O(1) rather than array filtering/spreading.prevSet.size === 0to skip re-rendering if already empty.Note on Storybook:
stories/useSet.story.tsxalready exists and covers all actions. Since this is an internal performance refactor with no public API changes, is an additional story needed?Type of change
Checklist
yarn test)yarn lint). Fix it withyarn lint:fixin case of failure.yarn lint:types).