refactor: use Set instead of Array for O(1) listener operations in singleton-handler#187
Merged
NathanZlion merged 1 commit intocloudscape-design:mainfrom Jan 28, 2026
Conversation
…ngleton-handler
Replace Array with Set in createSingletonHandler for managing listeners.
This improves performance for component mount/unmount operations:
- listeners.add() is O(1) vs Array.push() O(1) - same
- listeners.delete() is O(1) vs Array.indexOf() + splice() O(n)
- listeners.size is O(1) vs Array.length O(1) - same
This is significant because useMutationSingleton (used by useCurrentMode,
useDensityMode, useReducedMotion) watches document.body with
{ attributes: true, subtree: true }, firing on every DOM attribute change.
Component unmounts in apps with many listeners benefit from O(1) removal.
Sets maintain insertion order (ES2015+), preserving iteration consistency.
Added tests for:
- Listener notification order preservation
- Rapid mount/unmount cycle handling
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #187 +/- ##
=======================================
Coverage 99.00% 99.00%
=======================================
Files 42 42
Lines 1101 1101
Branches 280 296 +16
=======================================
Hits 1090 1090
+ Misses 11 10 -1
- Partials 0 1 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
pan-kot
approved these changes
Jan 27, 2026
NathanZlion
approved these changes
Jan 27, 2026
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.
This PR replaces the
Arraywith aSetincreateSingletonHandlerfor managing listeners, improving the time complexity of listener removal from O(n) to O(1).Motivation
The
createSingletonHandleris used byuseMutationSingleton, which powersuseCurrentMode,useDensityMode, anduseReducedMotion. These hooks observedocument.bodywith{ attributes: true, subtree: true }, meaning the MutationObserver fires on every attribute change anywhere in the DOM.In applications with many Cloudscape components using these hooks, the listener collection can grow large. The previous implementation used:
This is O(n) for each component unmount. With the Set-based approach:
Removal is O(1).
Changes
src/internal/singleton-handler/index.ts: ReplaceArraywithSetfor listener storagesrc/internal/singleton-handler/__tests__/create-singleton-handler.test.tsx: Add tests for listener order preservation and rapid mount/unmount cyclesComplexity comparison
push()add()indexOf()+splice()delete()lengthsizeBy submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.