9

Within instances of objects I like to use the closure mechanism to simulate private member variables. For a potential large number of created objects I don't need some of the private members though, but I have to declare them for them to be used in the closure, like "one", "two" and "three" here:

    var obj=function()
    {
        var one;
        var two;
        var three;

        var M=function()
        {
            one=5;
        };
    };

(Don't mind that this is not actually a working example of my setup, it's just to demonstrate the use of the closure over the three vars with M.)

Do the var statements themselves already consume memory, or does that depend on actually assigning something to these vars like with "one"?

2
  • In this context it might be interesting to read about what us happening when JavaScript interpreters parse the code or are calling a function. Read about Execution Contexts in the specification Jan 7, 2012 at 12:04
  • Although you're probably just worrying too much (microoptimization can happen with memory consumption as well), the answers could also be quite interesting for those who like to peek under language implementation hoods for the heck of it (such as me). +1
    – user395760
    Jan 7, 2012 at 12:14

3 Answers 3

7

The interpreter has to store information about scope - one = 5 will change the local variable one instead of creating a global variable (which would happen with e.g. four = 5). This information must cost some memory somehow. This memory usage also applies before assigning a value to one, because the information has to be available at the time you're assigning.

How much memory it will cost is difficult to say since it differs per interpreter. I guess it isn't enough to worry about.

Note that two/three are not used at all and may be garbage collected in this example. (Actually, you don't expose M either, so everything may be garbage collected right away in this example.)

7

When declaring a variable without assigning a value to it, there still needs to be some memory available for it, otherwise you cannot make a reference to the variable later in the program. I don't think it's a noticeable amount of memory being used and won't make a difference.

1
  • 1
    Hi Tincan, welcome to stackoverflow, and thanks for the answer. The other answers just beat you to it regarding information provided, so I'll just vote for all three ;) Jan 7, 2012 at 13:45
4

When declaring a variable, a memory space is reserved for it and allow you to store to or retrieve from that memory with the name you chose for the 3 variables. Such a space is empty until you fill it with a value (two/tree will remain null). This is performed with the assignment operation. The assignment operation gives a value to a variable.

4
  • 1
    Are you sure that just declaring it (and not assigning anything yet) already reserves memory? Jan 7, 2012 at 12:13
  • 3
    A variable is an amount of memory space reserved to store a piece of information of your code. To use such a memory space, you must first let the browser know that you would need it. For the browser to reserve such a memory space for you, and to use it eventually, you must give it a name. The memory space that is reserved will be used to receive some values from your application and to make such values available when your code needs them. The content of this reserved memory space can change or vary regularly.
    – NT88
    Jan 7, 2012 at 12:18
  • 2
    @Wolfgang: Yes, for each variable declaration, a binding is created in the environment record for each execution. This binding must use some memory. Have a look at the link (and the following sections) I posted in my comment. It's rather technical but maybe it helps. Jan 7, 2012 at 12:20
  • @FelixKling - can you provide the link that you mentioned here? I had a very similar doubt about declaration of a JS variable holding some memory or not. Cheers!
    – peevesy
    Jan 25, 2020 at 20:06

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.