Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8477995
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T18:37:19+00:00 2026-06-10T18:37:19+00:00

Question came up while working on following homework: Create 4 divs who change their

  • 0

Question came up while working on following homework: Create 4 divs who change their color when I click on a button.

It’s all working but my teacher uses the init function for getting the divs via getElementById("div1") and I use the changeColorFunction() itself and did not use an init function at all. See below:

My solution:

function changeColor () {

    document.getElementById("div1").style.background = createNewColor();
    document.getElementById("div2").style.background = createNewColor();
    document.getElementById("div3").style.background = createNewColor();
    document.getElementById("div4").style.background = createNewColor();

}

My teachers solution:

    var d1,d2,d3,d4;

    function init(){

    d1 = document.getElementById("d1");
    d2 = document.getElementById("d2");
    d3 = document.getElementById("d3");
    d4 = document.getElementById("d4");
    }
window.onload = init;

Its just a question about the best time/location for getting the elements.
With this little piece of code I guess not that big a difference but when DOES it matter?

When this project grows will the user ever recognize with my solution that the browser always has to get the elements first before code can run on them?

Hope you can follow.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T18:37:21+00:00Added an answer on June 10, 2026 at 6:37 pm

    If you are going to access those divs many times, it may be beneficial to cache them beforehand, which is what your teacher is doing.

    That is, it’ll be faster to change the style of a cached DOM element, stored in variable, then to fetch it from the DOM tree, using getElementById, everytime you want to change its style.

    The difference, however, is almost negligible, but as always, the best way to see it, is to benchmark it. I’ve created a jsperf.com test case to illustrate it. You can check here the results and play with it:

    Testing in Chrome 21.0.1180.83 32-bit on Windows Server 2008 R2 / 7 64-bit
    +-----------------------+------------------------------------------------+
    | Test                  |  Ops/sec                                       |
    +-----------------------+------------------------------------------------+
    | Cached getElementById |  584,606 ±2.62% fastest                        |
    +-----------------------+------------------------------------------------+
    | getElementById        |  422,957 ±1.27% 27% slower                     |
    +-----------------------+------------------------------------------------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While studying for a Functional Programming exam, I came across the following question from
This question came to my head while working with a map in silverlight that
This question came to my mind while working on 2 projects in AI and
So, while working on something that was having locking issues, a question came to
While looking at some conceptual questions in C,I came across this question in a
While studying for a Functional Programming exam, I came across the following questions from
This question came into my mind while generating sample data for a SO-answer. I
A while ago I asked a question about square detection and karlphillip came up
I've been working on iPhone application for quite a while now, and, I've came
This question came to mind while I was writing a class that iterates over

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.