Possible Duplicate:
How is the memory allocation done for variables in scripting languages?
In javascript, since it is a loosely typed language, when I write the code:
var foo;
Exactly after this step how many blocks of memory is allocated. I din assign anything to it and also when I type
var foo = 10;
and
var foo = "this is random"
in both cases how many bytes of memory is allocated?
I’m not sure about the specifics, which are implementation dependent, but here’s what I’d guess.
Simply doing
var foo;doesn’t create any new objects. However, foo might need to be added to a dictionary of declared variables in which case space for the stringfooand possible metadata is allocated, assuming it isn’t interned.When you do
var foo = 10;orvar foo = "this is random"it creates string or integer variables respectively, which would normally take up memory. However small ints and constant strings are almost certainly interned. Plus the JIT might optimize away the number reference to a plain machine integer, which case it doesn’t take any extra memory at all.Anyway, it’s hard to say much specific about performance in a high level language with varying implementations, especially when a JIT is involved.