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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T14:41:36+00:00 2026-05-11T14:41:36+00:00

In a particular script I’m writing, I have a number of objects which are

  • 0

In a particular script I’m writing, I have a number of objects which are linked to some DOM Elements. Given that each element has a unique id, should each object keep just the element’s id (and use document.getElementById each time), or store the element in a property?

Here’s a simplified example of what I mean:

function myThing(elId) {     this.elId = elId; } myThing.prototype.getElValue = function() {     return document.getElementById(this.elId).nodeValue; };  // -- vs -- //  function myThing(elId) {     this.el = document.getElementById(elId); } mything.prototype.getElValue = function() {     return this.el.nodeValue; }; 

Does it make any difference? Are there any performance issues I should know about?

  • 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. 2026-05-11T14:41:37+00:00Added an answer on May 11, 2026 at 2:41 pm

    I would store the element; it tends to make code clearer when you’re not calling document.getElementById all the time, and though in your case you may not need to change IDs or allow elements without IDs, it’s pretty common to want to do so.

    (Unlike apphacker I wouldn’t expect huge efficiency improvements from doing so, as getElementById tends to be fairly well-optimised in browsers.)

    Are there any performance issues I should know about?

    References from JavaScript objects to DOM objects are fine on their own, but when a DOM object has a link back to such a JavaScript object (usually through an event handler), you’ve got a reference cycle. This causes IE6-7 to leak memory as it fails to free up the cycled objects. For small simple apps you may not care. For complicated, long-running apps, you might have to work around the problem, for example by indirecting every event handler through a lookup map/array so there is no direct reference from the DOM object to the real event handler.

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

Sidebar

Related Questions

I have this particular script that runs so that the flash elements don't show
I am writing a script that requires checking whether a particular commit is a
I want to write a sh/bash script that can determine whether a particular directory
I am using the Command builder in scons to specify that a particular script
I have a script that listens to a jabber server and responds accordingly. Though
I have a shell script which I execute frequently in a day with different
When I run a particular SQL script in Unix environments, I see a '^M'
In particular what strengths does it have over caching features of Asp.net
In particular, would it be possible to have code similar to this c++ code
In particular, I'm editing the AutoCompletion.plist file for CSSEdit (if that even matters). My

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.