Skip to content

Conversation

obonelli
Copy link

Description

This PR optimizes the internal implementation of useSet and useMap hooks to reduce unnecessary allocations while preserving their existing API and test compatibility.

  • useSet

    • Previously created new Set via spread/Array.from on every operation.
    • Now keeps a stable Set in a ref and mutates it directly, forcing re-render with a reducer.
    • Public API remains unchanged (add, remove, toggle, reset, clear, has).
  • useMap

    • Previously rebuilt object with spread on every set or remove.
    • Now stores a stable object in a ref and updates in place, with reducer re-renders.
    • Public API remains unchanged (get, set, setAll, remove, reset).

Type of change

  • New feature (non-breaking change which adds functionality/performance improvement)

Checklist

  • Perform a code self-review
  • Ensure the test suite passes (yarn test)
  • Make sure code lints (yarn lint) and types are fine (yarn lint:types)
  • Comment the code, particularly in hard-to-understand areas
  • Add documentation
  • Add hook's story at Storybook
  • Provide 100% tests coverage (existing suite already covers most scenarios)

@obonelli
Copy link
Author

Hi 👋

This PR improves useSet and useMap by reducing allocations while keeping the exact same API and passing all existing tests

image
  • No breaking changes.
  • Performance win on frequent add/remove/toggle/set operations.
  • Fully covered by the current test suite.

Happy to adjust if reviewers prefer a different approach 🙌

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant