The Nuthouse
The Nuthouse

Reputation: 173

Does JavaScript addEventListener(); slow down a program?

Disclaimer: I don't know if this is a good site to ask this question on. I'm making an algorithm that gets triggered by addEventListener();in vanilla JavaScript. I want the algorithm to work as quick as possible, but I want there to be control over when it's executed (reason for event listener). My question is does the window continuously check if the event has happened yet, or rather only check when the event trigger has been activated, e.g. a click, or button press.

Upvotes: 0

Views: 851

Answers (2)

arryph
arryph

Reputation: 2825

Mouse, Keyboard clicks are registered in Macrotask queue in the eventloop, on each iteration, eventloop checks Macrotask queue, if found any item executes the first one and after it is executed fully, eventloop execute tasks in microtask queue. Multiple queue is managed to prioritize tasks.

Upvotes: 1

Barmar
Barmar

Reputation: 782099

Events are queued. When JavaScript returns to the main event loop, the events are processed. There's no continuous polling, and nothing interrupts a script while it's running.

Upvotes: 2

Related Questions