1

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?

Benno
  • 3,008
  • 3
  • 26
  • 41
  • I wanted advice on garbage collection and any global scope issues before I began changing it over. Theres ~3400 lines of code all up in here so, so it'd take a while lol. – Benno Aug 19 '11 at 02:48

3 Answers3

1

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.

hvgotcodes
  • 118,147
  • 33
  • 203
  • 236
  • Thanks for the insight. I notice you used prototype.instanceMethod, so would that mean MyClass.prototype.displayContacts? or inside the function, this.prototype.displayContacts? What are the benefits of using .prototype? I've been a bit confused on that as well :p – Benno Aug 19 '11 at 02:49
  • @benno -- look at this question: http://stackoverflow.com/questions/4650513/why-is-javascript-prototyping/4650576#4650576 – hvgotcodes Aug 19 '11 at 02:50
  • thanks for that. At the moment I'm not sure if prototyping the methods will make much difference as there's only one call to the object, but, I'm intending on splitting it up into more manageable chunks (i.e. order object and products' object), so prototype would be much more beneficial there I'm guessing when I have multiple product objects. Cheers for that! Will save a lot of performance down the track! – Benno Aug 19 '11 at 03:00
  • A better approach would be to make *CODES* a property of the prototype, so instances can access it as `this.CODES`. – RobG Aug 19 '11 at 04:52
1

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.

Schleis
  • 41,516
  • 7
  • 68
  • 87
1

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.

rugg
  • 531
  • 3
  • 9
  • Thanks for that. Yeah I wasn't too sure how garbage collection would handle having many global scope variables as opposed to non-globals. I'm just trying to optimise as much as I can because it gets a tad laggy after a while. – Benno Aug 19 '11 at 02:51