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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T17:06:23+00:00 2026-05-24T17:06:23+00:00

So I have a rather large object orientated javascript class, with about 120 functions

  • 0

So I have a rather large object orientated javascript class, with about 120 functions (a lot of getters and setters). Some of these functions have variables that are basically constants.

What I’m wondering, is should I declare these variables in a global scope of the object, so every time the function is run it doesn’t have to re-declare the variable?

An example function is below. this.displayContacts is run several times (and will always run within the object), so in this case, there’s no point declaring the ‘codes’ object inside the function?

function orderObject() {

  this.displayContacts = function() {
    var codes  = {'02':'02','03':'03','07':'07','08':'08'};
       // do something with codes
  };

}

So, would this be better, performance wise?

function orderObject() {
var codes  = {'02':'02','03':'03','07':'07','08':'08'};
  this.displayContacts = function() {
    // do something with codes.
  };

}

My other concern is that if I end up with a lot of global variables/objects inside the main orderObject, will that be MORE of a performance hit than simply re-declaring the variables each time?

  • 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-24T17:06:24+00:00Added an answer on May 24, 2026 at 5:06 pm

    absolutely.

    function MyClass() {
       this.somevar = ''; // instance scoped variable
    };
    
    MyClass.CODES = {'02':'02'...}; // 'Class' scoped variable; one instance for all objects
    
    MyClass.prototype.instanceMethod = function(){
      // 'this' refers to the object *instance*
      // it can still use MyClass.CODES, but can also account for variables local to the class
    }
    

    CONSTANT is ‘static’ so to speak, in java-talk. If your codes are global to the class (and the rest of your application), you will save a lot of overhead this way — only define the object once. Note that you can have ‘static’ class-level methods as well, for those cases where the function doesn’t need to operate on variables specific to an instance of the class.

    Unless your app is really beefy, performance optimization probably wont make it noticeably faster. But that doesn’t mean that OO design is not worth-while — if you are going to use javascript in an object oriented way, its not too hard and never a bad idea to use good OO principals.

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

Sidebar

Related Questions

I have a rather large, dynamic sparse matrix object class to write, and I
Suppose I have a rather large class Matrix , and I've overloaded operator== to
I'm trying to implement a rather large object that implements many interfaces. Some of
I have some data which I originally stored in a generic Javascript object, with
I have data database containing some rather large strings, each of which holds a
In Linux, I have a rather large file with some extraneous information tacked on
I have rather large input XML files that must be converted to a given
I have a rather large file (150 million lines of 10 chars). I need
We have a rather large SVN repository. Doing SVN updates are taking longer and
I have a rather large (many gigabytes) table of data in SQL Server that

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.