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 6320599
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T16:00:13+00:00 2026-05-24T16:00:13+00:00

I’m building one of my first web apps using HTML5, specifically targeting iPhones. Since

  • 0

I’m building one of my first web apps using HTML5, specifically targeting iPhones.

Since I’m pretty new to this, I’m trying to develop some good coding habits, follow best practices, optimize performance, and minimize the load on the resource-constrained iPhone.

One of the things I need to do frequently… I have numerous divs (each of which has a unique id) that I’m frequently updating (e.g., with innerHTML), or modifying (e.g., style attributes with webkit transitions and transforms).

In general – am I better off using getElementByID each time I need a handle to a div, or should I store references to each div I access in “global” variables at the start?

(I use “global” in quotes because I’ve really just got one truly global variable – it’s an object that stores all my “global” variables as properties).

I assume using getElementByID each time must have some overhead, since the function needs to traverse the DOM to find the div. But, I’m not sure how taxing or efficient this function is.

Using global variables to store handles to each element must consume some memory, but I don’t know if these references require just a trivial amount of RAM, or more than that.

So – which is better? Or, do both options consume such a trivial amount of resources that I should just worry about which produces more readable, maintainable code?

Many thanks in advance!

  • 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-05-24T16:00:15+00:00Added an answer on May 24, 2026 at 4:00 pm

    “In general – am I better off using getElementByID each time I need a handle to a div, or should I store references to each div”

    When you’re calling getElementById, you’re asking it to perform a task. If you don’t expect a different result when calling the same method with the same argument, then it would seem to make sense to cache the result.

    “I assume using getElementByID each time must have some overhead, since the function needs to traverse the DOM to find the div. But, I’m not sure how taxing or efficient this function is.”

    In modern browsers especially, it’s very fast, but not as fast as looking up a property on your global object.

    “Using global variables to store handles to each element must consume some memory, but I don’t know if these references require just a trivial amount of RAM, or more than that.”

    Trivial. It’s just a pointer to an object that already exists. If you remove the element from the DOM with no intention to use it again, then of course you’ll want to release your hold on it.

    “So – which is better? Or, do both options consume such a trivial amount of resources that I should just worry about which produces more readable, maintainable code?”

    Depends entirely on the situation. If you’re only fetching it a couple times, then you may not find it worthwhile to add to your global object. The more you need to fetch the same element, the more sense it makes to cache it.


    Here’s a jsPerf test to compare. Of course size of your DOM as well as length of variable scope traversal and the number/depth of properties in your global object will play some role.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

That's pretty much it. I'm using Nokogiri to scrape a web page what has
We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
Specifically, suppose I start with the string string =hello \'i am \' me And
Seemingly simple, but I cannot find anything relevant on the web. What is the

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.