Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

199
Visualizações
What has a greater performance impact: adding an extra event listener, or needing to query the DOM repeatedly?

I'm trying to understand the best way to optimize performance in JavaScript. To simplify, I have some code where a bunch of buttons are each associated with a specific element, and I need to be able to make changes to both the button and its associated element whenever a button is clicked.

I have been told it is better to consolidate event handlers with event delegation where you can. I've also been told if I can avoid querying the DOM over and over for the same element, better to do it just once and store that for later. But in this case, it seems like I need to do one or the other, so I'm wondering which is more costly? Currently I have the following code that runs once to set things up:

buttons.forEach((button) => {
  const area = document.querySelector(`#${button.getAttribute('aria-controls')}`);   
        
  button.addEventListener('click', (e) => {
    // do some things with "area" and "button"
  });
});

Obviously this creates an individual listener for each button, so I was thinking I could rewrite it like this:

document.addEventListener('click', (e) => {
   if(e.target.classList.contains("button-class")) {
      const area = document.querySelector(`#${e.target.getAttribute('aria-controls')}`);
      // do some things with "area" and "button"
   }
});

But is this worth the trade off that now every time a button is clicked I have to query for the associated "area" element again? Does the answer change depending on how many buttons I have or how often they are likely to be clicked? Is there some third answer that is more optimal than either of my solutions? Apologies if this is a really obvious and stupid question, but I've been googling and looking through other Stack Overflow questions and really can't find anything to answer this.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

which is more costly?

As you already noticed, it's a trade-off. The costs you need to compare are respectively

  • the execution time taken for initialisation
  • the execution time taken of each click handler
  • the memory required to keep state

For just a few buttons, that are clicked in normal web flows (and not, say, an APM testing tool), it really doesn't matter.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda