Syed Mehtab Hassan
Syed Mehtab Hassan

Reputation: 1337

how much memory is required for var in javascript and when it is allocated?

As in java

int requires 4 bytes
float requires 4 bytes
char requires 2 bytes

but in javascript we only have variable with type var.

Q1. How much memory var requires?

Q2. When this memory is allocated to the variable?

As in the below example different type of data is allocated to same variable

var a = 1;
console.log(a, typeof a);
a = "javascript";
console.log(a, typeof a );

Upvotes: 6

Views: 3014

Answers (2)

Justin Meskan
Justin Meskan

Reputation: 683

There is a good conversation going on here that might help you out.

Storing primitive datatypes in javascript

Upvotes: -1

Jonas Wilms
Jonas Wilms

Reputation: 138257

How much memory var requires?

We will never know. It can range from nothing to a few bytes. That depends on the engine, and the optimization steps it takes. Take this example:

 function test() {
   var unused = "stuff";
 }

In this case unused cannot be used, and the compiler might decide to completely optimize it away, then it takes up no memory. However if you add a breakpoint in that function, it has to fall back to an unoptimized version and has to allocate unused.

Or take this:

function addOne(n) { return n + 1; }

var test = addOne(2);
test = addOne(3);

The compiler could optimize it in a few ways:

1) It doesn't optimize anything, then n could contain any possible JavaScript value, therefore it is probably referencing some kind of "value" superclass. The reference will take up a few bytes, so does the class instance itself.

2) It assumes that n is always a number (cause it saw that from the calls), and generates optimized bytecode that only allocates a number (or an integer) onto the stack. That will take up 8 bytes (maybe less / more).

3) It completely inlines the function, n doesnt exist at all. The resulting code is:

 var test = 2 + 1;
 test = test + 1;

Now if you however do addOne("test") it has to either fall back to an unoptimized version, or generate a new optimized version for strings.

As that is what all (most) engines are doing, you never know how much space a variable does take, as it can change (multiple times) at runtime. It can even change depending on how you call it, wether you call eval or with somewhen, and wether you use the debugger to "look into" the functions.

For global variables (like in your case) it is probably not possible for the engine to do optimizations (as so many parts of the code can interact with it), so probably case 1 applies.

When this memory is allocated to the variable?

Either never, or when you call the function, or for the global scope: when the code gets parsed, or when the assignment gets reached.

 // assuming a non optimized case:
function test() { // a reference holder for a gets allocated
   let a;
   a = {}; // an object gets allocated, a reference to it gets stored in the already allocated slot
}

The optimizations above are well documented for V8, which runs in Chrome and NodeJS among others.

Upvotes: 4

Related Questions