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

190
Vistas
Javascript variables and DOM Selectors

I have a question about DOM selectors.

Is there a performance difference between;

This code:

if (document.getElementById("post-dropdown-menu-" + postID) && document.getElementById("post-dropdown-menu-" + postID).style.display != "none") {
            document.getElementById("post-dropdown-menu-" + postID).fadeOut(200);
            return false;
} 

And this code:

var neededElement = document.getElementById("post-dropdown-menu-" + postID);

if (neededElement && neededElement.style.display != "none") {
            neededElement.fadeOut(200);
            return false;
} 

I really want to know if browser or javascript doing DOM Select proc. again if element already used in a function.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

Of course the second one is faster because you only use query selector once which mean less processing.

and here is a test score using JSBench.

enter image description here

Note that the effect will never be noticed except if you are using this inside a loop or so.

about 4 years ago · Juan Pablo Isaza 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