3

I'm currently doing a lot of development in OOP Javascript. In particular, I'm dealing with coordinates and dimensions a lot and I have many variables defined, as objects, like so:

coords = {
    x:10,
    y:15
};

dimensions = {
    width:500,
    height:250
}

But I'm wondering if it would be quicker/more efficient to specify the values as separate, primitive, variables:

coordX = 10;
coordY = 15;

dimWidth = 500;
dimHeight = 240;

Could someone please briefly explain the advantage/disadvantage of each method of string the variables? Usability-wise, I find objects easier, as you can group related values together; although I gather that it is slower. But is it slower, or take up more memory, than defining more variables?

Cheers.

EDIT: Wow! I never expected this many responses, especially so quickly! Thank you all for your responses.

It seems like any performance differences are negatable, but this may only apply to simple scripts. What I have is a constant loop, which needs to run at the highest FPS possible. Each loop uses many object variables for storing data, like above. So is there the potential for performance issues in these circumstances?

GreenImp
  • 80
  • 9
  • I would be beyond astonished if your application actually showed any noticeable difference in performance between the two. – lonesomeday Feb 04 '12 at 23:10
  • @The Nail: Not at all. That one is a question for Java, not JavaScript. Please read more carefully. – Platinum Azure Feb 04 '12 at 23:11
  • @FairyLee: this borders on [premature optimization](http://c2.com/cgi/wiki?PrematureOptimization). – outis Feb 04 '12 at 23:18
  • Near duplicate of ["array of variables or separate variables?"](http://stackoverflow.com/q/4267046/90527) – outis Feb 04 '12 at 23:20
  • Somewhat related: http://stackoverflow.com/questions/8423493/what-is-the-performance-of-objects-arrays-in-javascript-specifically-for-googl (I also removed my previous comments to make space for new comments.) – The Nail Feb 04 '12 at 23:23
  • @TheNail Cheers for that related link. The marked answer was very helpful! – GreenImp Feb 04 '12 at 23:57
  • To deal with performance issues, [profile](http://stackoverflow.com/q/855126/) the script and deal with the algorithmic inefficiencies. Minor things such as this should be addressed last and only if other improvements don't prove sufficient and profiling suggests there will be a benefit. – outis Feb 05 '12 at 00:25
  • @outis cheers for the link - again, a very helpful resource! I'm going to look into profiling with JS, haven't actually done it before. – GreenImp Feb 05 '12 at 00:53

2 Answers2

4

You use whatever is most appropriate for the problem at hand.

If it makes sense in your code to use an object, use one. If variables make more sense, use them.

  • Thanks for your answer, that's pretty much what I'm doing at the moment, but my script is so extensive and is chomping away at CPU, so I'm looking to better performance wherever possible. – GreenImp Feb 04 '12 at 23:59
2

Performance should not be an issue - the properties are declared on the object and no prototype chain is necessary to access them (also javascript engines are becoming more and more performant in this area).

I'd personally prefer to use the OO approach in this situation, since it allows a logical grouping of values, which will be clearer to any other developers who look at your code (and, if you're anything like me, possibly you in a couple of months time!).

Rich O'Kelly
  • 41,274
  • 9
  • 83
  • 114
  • Thanks. I prefer to use Objects in cases like this, as it's 'neater'. I'm just trying to ascertain if their would be any noticeable performance change between the different methods. Cheers – GreenImp Feb 05 '12 at 00:03
  • 1
    Not only that, but it's easier to expand objects in the future, where a string of variables can get messy. – Jeffrey Sweeney Feb 05 '12 at 01:38
  • I've marked this as the accepted answer simply because it explains a bit more than the other, even though they are stating pretty much the same things. Thanks to all who helped out. – GreenImp Feb 05 '12 at 12:27