Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

308
Vistas
Does adding too many event listeners affect performance?

I have a general question about javascript (jQuery) events/listeners. Is there any limit for the number of click listener without getting performance problems?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

In terms of performance, the number of elements the event is bound to is where you'd see any issues.

Here is a jsperf test. You'll see that binding to many elements is much slower, even though only one event is being bound in each case.

The 3rd test in the jsperf shows how you'd bind the event to a parent element and use delegation to listen for the elements you want. (In this case .many)

n.b. The test shows that the 3rd option is the fastest, but that's because it's targeting the element with an id and not a class.

Update: Here's another perf test showing 3 events bound to an id vs one event bound using a class

over 4 years ago · Santiago Trujillo Denunciar

0

Though this is an old question, I do not feel that it's completely answered yet.

As atmd pointed out: It's already important where you're adding the event handlers to. But the original question seems to be more concerned about the performance impact of triggering those event handlers (e.g. click or scroll events).

And yes, adding additional event handlers to an element DOES decrease performance. Here is a performance comparison to test the following cases:

https://jsbench.me/ztknuth40j/1

The results

  1. One <div> has 10 click handlers, and the click event is triggered by jQuery.
    → 72.000 clicks/sec
  2. One <div> has 100 click handlers, and the click event is triggered by jQuery.
    → 59.000 clicks/sec ~ 19% slower than first case
    This shows, that additional event handlers can slow down the execution
  3. One <div> has 10 click handlers, and the click event is triggered by plain JS.
    → 84.000 clicks/sec ~ 6% faster than the first case
    Using plain JS is a little bit faster than using jQuery
  4. One <div> has 100 click handlers, and the click event is triggered by plain JS.
    → 14.000 clicks/sec ~ 77% slower than second case
    This is interesting: When using native events, the number of listeners seems to degrade the performance faster than using jQuery.

(Those results vary on every run and depend largely on your hardware and browser)

Keep in mind that those tests are done with an empty function. When adding a real function that performs some additional tasks, the performance will slow down even further.

Here is a second test that changes the contents of a div on each click:

https://jsbench.me/ztknuth40j/2

Is it slow?

On the other hand: Even 100 operations per second are super fast (it means, that every event handler is executed 100 times in a single second) and no user will notice the delay.

I think you will not run into problems with user-action events like click or mouseenter handlers, but need to watch out when using events that fire rapidly, like scroll or mouseover.

Also, as computers get faster and browsers apply more and more optimizations, there is no hard limit for how many event handlers are "too much". It not only depends on the function that's called and the event that's observed but also on the device and browser of the user.

over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda