-
Notifications
You must be signed in to change notification settings - Fork 47.2k
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
Create Synthetic Events Lazily #19909
Conversation
): void { | ||
const bubbleName = event._reactName; | ||
const captureName = bubbleName !== null ? bubbleName + 'Capture' : null; | ||
reactName: string, |
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 not nullable now so we don't need extra checks below.
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit e3dde61:
|
const listeners: Array<DispatchListener> = []; | ||
|
||
const eventListeners = getEventHandlerListeners(currentTarget); | ||
if (eventListeners !== null) { | ||
const targetType = ((event.type: any): DOMEventName); |
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.
Not sure if the discrepancy (the code path above reads event.nativeEvent.type
instead) was intentional but for now I'm keeping it.
Details of bundled changes.Comparing: 0a00804...e3dde61 react-dom
ReactDOM: size: 0.0%, gzip: -0.0% Size changes (experimental) |
Details of bundled changes.Comparing: 0a00804...e3dde61 react-dom
ReactDOM: size: 0.0%, gzip: -0.0% Size changes (stable) |
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.
Looks good to me :)
Currently we create them very aggressively — especially with the eager change — but many may not end up being used. While the allocation itself is not a significant cost, we are actually running a bunch of code there, including iterating over the event interface, copying every known property into
this
, and running our normalization polyfills. In some cases, our polyfills also do some other extra work (e.g. some calculations).However, this is just an artifact of the initial code structure. We don't actually need to create the synthetic events early except the enter/leave case where it's much more convenient. In other cases, we can make them lazy and create if we actually found some listeners. This also lets us delay any other extra work.
I should note that with a throttled CPU, the SyntheticEvent constructor takes a noticeable amount of time for noops, which is why I want to optimize this.