Jump to content

  • Log In with Google      Sign In   
  • Create Account


Javascript - Better to instantiate variables, or leave them undefined?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
4 replies to this topic

#1 BUnzaga   Members   -  Reputation: 265

Like
1Likes
Like

Posted 14 October 2012 - 06:55 AM

In Javascript, if I know a variable type, but not necessarily the value, is it better on memory or run-time performance to instantiate it with a 'dummy' value, or is it better to just leave it undefined, and then only instantiate it when it is first needed?

For example...

var array1;
var number1;
var string1;
/* vs */
var array2 = [];
var number2 = 0.0;
var string2 = "";

// later in code...
function onInit(){
array1=[1,2,3];
array2=[1,2,3];
number1 = 3.142;
number2 = 2.718;
string1 = "Hello";
string2 = "World";
}

Is it best to fill the variable with a dummy 'type' or just leave it 'undefined' ?

Sponsor:

#2 slicer4ever   Crossbones+   -  Reputation: 3703

Like
3Likes
Like

Posted 14 October 2012 - 08:05 AM

it is better to initialize to a "null" value, so this way you can do error checking against this null value, to make sure you don't incorrectly try to access invalid data.

memory-wise, you are not saving anything, the variable still takes up physical memory, regardless of it's content.

performance-wise, it's doubtful you would see any return whatsoever, any performance gain(and this would be the upmost minimal), would almost never translate to being a bottleneck point for an application, as such, it's pretty much a moot point to discuss.

Edited by slicer4ever, 14 October 2012 - 08:05 AM.

Check out https://www.facebook.com/LiquidGames for some great games made by me on the Playstation Mobile market.

#3 BUnzaga   Members   -  Reputation: 265

Like
0Likes
Like

Posted 15 October 2012 - 07:25 AM

Thanks slicer4ever, that is a good idea about setting them to null for error checking.

#4 Telcontar   Members   -  Reputation: 893

Like
0Likes
Like

Posted 15 October 2012 - 08:40 AM

Slicer pretty much covered it, but I will reinforce the importance of never intentionally leaving your data in an invalid state. That's just asking for trouble. Set it to something that won't throw an error upon an attempt to access it, even if it is a "dummy" value.

I Create Games to Help Tell Stories | Writing Blog


#5 tjlahr   Members   -  Reputation: 107

Like
1Likes
Like

Posted 19 October 2012 - 10:59 PM

It's perfectly valid to leave your variables 'undefined' at the top of your scope. In fact, it's what the JavaScript interpreter does anyway though a mechanism called variable hoisting. Devs often do this explicitly at the top of their scope in a single, comma-seperated list like this:

[source lang="jscript"]function(){ var array, number, string; // or var array = [], number = 0, string = ''; // function body...}[/source]
I prefer the latter method, because it helps the routine document itself.

As for testing against null: I'd be careful. It's one of JavaScript's many quirks that null and undefined are strage cats and can behave unexpectedly (more info here). Everyone's needs are different but I would reccomend using if(typeof array !== 'undefined') if you must check for variable initialization.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS