Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!