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?

8
  • I would be beyond astonished if your application actually showed any noticeable difference in performance between the two. Commented Feb 4, 2012 at 23:10
  • @The Nail: Not at all. That one is a question for Java, not JavaScript. Please read more carefully. Commented Feb 4, 2012 at 23:11
  • @FairyLee: this borders on premature optimization. Commented Feb 4, 2012 at 23:18
  • Near duplicate of "array of variables or separate variables?" Commented Feb 4, 2012 at 23:20
  • Somewhat related: stackoverflow.com/questions/8423493/… (I also removed my previous comments to make space for new comments.) Commented Feb 4, 2012 at 23:23

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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.
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!).

3 Comments

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
Not only that, but it's easier to expand objects in the future, where a string of variables can get messy.
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.