Sign in to follow this  

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

This topic is 1915 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

In [b]Javascript[/b], 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' ?

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Thanks slicer4ever, that is a good idea about setting them to null for error checking.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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 [url="http://www.adequatelygood.com/2010/2/JavaScript-Scoping-and-Hoisting"]variable hoisting[/url]. 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 ([url="http://bonsaiden.github.com/JavaScript-Garden/#core.undefined"]more info here[/url]). Everyone's needs are different but I would reccomend using if(typeof array !== 'undefined') if you must check for variable initialization.

Share this post


Link to post
Share on other sites
Sign in to follow this