question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[Suggestion] Replace EventDispatcher with native EventTarget and CustomEvents

See original GitHub issue

Is your feature request related to a problem? Please describe.

The current EventDispatcher is lacking a lot of functionality that the native EventTarget supports, such as .stopPropagation.

Describe the solution you’d like

I suggest that the custom EventDispatcher is replaced and having classes like Object3D extend the native EventTarget instead.

This would affect the event objects currently being dispatched, because they would have to be replaced with CustomEvent instances instead.

This would be a breaking change.

As for browser support, then it is the following for extending EventTarget:

  • Chrome: 64
  • Edge: 79
  • Firefox: 58
  • Internet Explorer: None
  • Safari: 14

Describe alternatives you’ve considered

Alternatively I think the EventDispatcher should be further developed to support relevant APIs like .stopPropagation.

Additional context

It was previously mentioned in #12854, that it would be able to be removed “soon”.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
redichecommented, May 18, 2022

I must admit I had completed missed that PR thread when looking into previous issues regarding this. The performance does look quite bad.

I checked the fiddle and it seems there have been quite some improvements on both Chrome and Firefox since the original test, but yes, it is still significantly worse.

Chrome:
native dispatch: 2.988037109375 ms
three dispatch: 0.47705078125 ms

Firefox:
native dispatch: 2ms - timer ended
three dispatch: 1ms - timer ended

I will close this issue, but I do think it’s worth keeping an eye on any performance improvements that the browsers make over time, since the native events come with a lot of features which could significantly improve developer ergonomics.

1reaction
LeviPesincommented, May 18, 2022

Here is the fiddle: https://jsfiddle.net/xm3jnkzd/ Three is still about 20 times faster than native on Chrome.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[suggestion] replace the EventDispatcher with EventEmitter
The idea of EventDispatcher is to provide the same javascript API ... [Suggestion] Replace EventDispatcher with native EventTarget and ...
Read more >
Custom Events Model without using DOM events in JavaScript
Just create a normal DOM EventTarget object in the constructor of your object and ... EventDispatcher = { events: {}, on: function(event, ...
Read more >
EventTarget.dispatchEvent() - Web APIs | MDN
The dispatchEvent() method of the EventTarget sends an Event to the object, (synchronously) invoking the affected EventListeners in the ...
Read more >
Dispatching custom events - The Modern JavaScript Tutorial
Found the answer: dispatchEvent runs synchronously, native dispatches run asynchronously. From MDN: Unlike "native" events, which are fired by ...
Read more >
Events | Node.js v19.3.0 Documentation
This behavior is deprecated and will change in a future release to align EventTarget with other Node.js APIs. Any code relying on the...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found