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

179
Vistas
What's better in terms of performance? Using addEventListener("click) in each element or add it to the document and differenciate them using e.target?

Just as I said in the title, I'm learning JS and I want my code to have the best performance possible. I saw a video in which the youtuber said that applying the addEventListener("click") to the document and using if statements to differenciate "how" activated the event is more effective and has a better performance than applying it individually

EXAMPLE

$document.addEventListener("click",(e)=>{
   if (e.target.matches("nav i img")){
       console.log("logrado");
       if (hamburgerMenuState == false){
           $hamburgerMenu.classList.add("left-zero");
           $hamburgerMenu.classList.remove("left-away");
           hamburgerMenuState = true;
       }
       else{
           $hamburgerMenu.classList.remove("left-zero");
           $hamburgerMenu.classList.add("left-away");
           hamburgerMenuState = false;
       }        
   }
   else if(e.target.matches("#hamburger-menu ul li a")){
       console.log("nahse")
       $hamburgerMenu.classList.remove("left-zero");
       $hamburgerMenu.classList.add("left-away");
       hamburgerMenuState = false;
   }
   else if(e.target.matches("#seccion1 #iniciarReloj")){
       $btnIniciarReloj.disabled = true;
       intervalo = setInterval(()=>{
           console.log(intervalo);
           let fecha = new Date();
           $document.getElementById("reloj").textContent = fecha.toLocaleTimeString();
       },1000)
   }
   else if(e.target.matches("#seccion1 #detenerReloj")){
       $btnIniciarReloj.disabled = false;
       clearInterval(intervalo);
       $document.getElementById("reloj").textContent = "";
   }
   else if(e.target.matches("#seccion1 #iniciarAlarma")){
       audio.play();
   }
   else if(e.target.matches("#seccion1 #detenerAlarma")){
       audio.pause();
       audio.currentTime = 0;
   }
}) 
about 4 years ago · Juan Pablo Isaza
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