ajmajmajma
ajmajmajma

Reputation: 14216

Javascript - memory usage with incrementing values

I am curious about this situation where you use a variable integer that you increment with the javascript increment ++, and how it affects memory usage. What I am curious about is if the variable is a pointer in the scenario below or if it will add to the stack when incremented using the ++. Having a hard time finding resources on this, so any input would be helpful.

let i = 0;

do {
  i++;
}
while (i < 10); 

So what I am wondering is with each ++ to the i variable, does this impact memory usage?

Upvotes: 0

Views: 186

Answers (2)

Mr.UV
Mr.UV

Reputation: 100

I don't think so it will impact memory usage much assuming the variable is small. Integer needed 4 bytes to be stored. with 4 byte we can store 0 to 4,294,967,295 numbers. In the case of variable getting incremented more than 4,294,967,295 it will consume more memory than 4 bytes to hold that large value ( like double or long with more memory).

That's my thoughts on it. Hope it will help :)

Upvotes: 1

Alex89Spain
Alex89Spain

Reputation: 76

A variable declaration "let i = 0" will use up the memory intended for a basic type. 32 bits or 64 bits, depending on your underlying architecture. The space in memory is the same for i = 0 all the way up to the max value possible for an integer. So no, incrementing an integer will not change the space that variable takes up in memory.

Upvotes: 1

Related Questions