Benno
Benno

Reputation: 3008

Object Orientated Javascript / Variable declarations / Performance

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?

Upvotes: 1

Views: 155

Answers (3)

hvgotcodes
hvgotcodes

Reputation: 120178

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.

Upvotes: 1

rugg
rugg

Reputation: 551

Garbage collection in JavaScript depends on the browser, and most modern browsers handle it pretty well. If you go ahead and make these global, you might see a slight performance increase simply because it's not executing that line of code every time. However, I can't imagine any significant increase in performance by making these static properties on the class, but if they don't change, then it would make more sense.

Upvotes: 1

Schleis
Schleis

Reputation: 43700

I would say that if you have something that you are using in multiple places that it should become a property of your object so that it doesn't have to be redeclared each time. It would also help make the maintenance of the object easier if that constant has to change. Then you are changing it only in one place and not having to hunt down all the locations where you used it.

Don't repeat yourself.

Upvotes: 1

Related Questions