-
Notifications
You must be signed in to change notification settings - Fork 46.9k
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
Use ES6 Map in ReactComponentTreeHook if available #7491
Conversation
@@ -292,12 +308,21 @@ var ReactComponentTreeHook = { | |||
}, | |||
|
|||
getRootIDs() { | |||
return Object.keys(rootIDs); | |||
var registeredIDs = ReactComponentTreeHook.getRegisteredIDs(); | |||
return registeredIDs.filter(ReactComponentTreeHook.isRootID); |
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.
This is a rare operation (I currently only use it in tree hook tests) so it’s fine to be slow.
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.
Can you rename it to getRootIDsSlow?
If perf is important then you should probably make sure that the Map implementation is native as well, polyfills are considerably slower as you might imagine. |
There's been a long standing TODO in |
I thought we could use it in one place, see how it goes, and gradually extract a helper if it seems worth it. ReactInstanceMap works in a different way though: its keys are objects so it has to put a field on them. This is another reason I didn't want to abstract prematurely. Different use cases across the codebase. Better to abstract after we actually see repeating patterns. |
lgtm. If the logic gets any more complicated let's make our own IntMap class with the same signature as Map so all the code doesn't need to branch. |
Addressed feedback:
|
@@ -184,9 +232,14 @@ var ReactComponentTreeHook = { | |||
// got a chance to mount, but it still gets an unmounting event during | |||
// the error boundary cleanup. | |||
item.isMounted = false; | |||
if (item.parentID === 0) { | |||
var indexInRootIDs = rootIDs.indexOf(id); |
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.
Can we do something that's not O(n^2) to unmount n roots? If getRootIDs is only used in tests than the slow implementation you had would be better.
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.
Ok, will do on Monday
* Use ES6 Map in ReactComponentTreeHook if available * Make getRootIDs fast again * Only use native Map (cherry picked from commit db452bd)
This makes DEV unmounting consistently faster in Chrome, Firefox, and Safari.
It’s because
purgeUnmountedComponent
becomes less intensive and doesn’t usedelete
s.Browsers that don’t have
Map
orArray.from
will fall back to using objects.We may want to try the same pattern for other global maps that we write/read often.
Chrome (before)
Chrome (after)
Firefox (before)
Firefox (after):
Safari (before)
Safari (after)