Could someone please compare and contrast between the costs of creating objects in JavaScript/JQuery and a simple variable declaration?
For Example:
var G = {}
G.obj = (function(){
var x = 10; //'private' attribute, can be accessed internally only
G.y = 20; //'public' property
});
x obviously has a local scope where as Y would be accessible publicly through G with a selector. Are there any significant overheads involved in the latter approach?
Thanks.
Edit: I realize this might sound like a silly question but I am planning an architecture for an HTML5 game, I have to keep little things like these in mind as I move forward.
First, you are not really executing the function in your code block.
Now, As far as cost of creating local variables v/s global properties is concerned, AFAIK, accessing(getting/setting) a global property would be slightly more expensive as you are traversing up the scope chain. The higher the property in scope chain, the more the cost. However, this is less of an issue IMHO.
The bigger issue is your choice of sticking things in a global store. Using local variables is helpful as local variables dont stick around after the function finishes execution. Therefore, you dont keep unnecessary state in memory. This IMO should drive your design on where to keep state for your application.