Analysis of heap memory and stack memory in JS

Source: Internet
Author: User
Tags true true

Recently, with the big guy in the group, the interview ran into a problem,

Q: Talk about the difference between Var, let, and const
A:balabalabalabla ...
Can the value defined by the Q:const be changed?
A: Are you kidding me? I can't.

I wonder what you crossing think? The answer is part can change, part cannot change. The basic type of the const definition cannot be changed, but the defined object can be changed by modifying the object's properties. Such as

>>> const a = 1>>> a<<< 1>>> a = 2<<< VM1750:1 Uncaught TypeError: Assignment to constant variable.    at <anonymous>:1:3    (anonymous) @ VM1750:1>>> const b = {}>>> b<<< {}>>> b.name = 1>>> b<<< {name: 1}>>> b = {}<<< VM1785:1 Uncaught TypeError: Assignment to constant variable.    at <anonymous>:1:4

Does const not define constants? Why can you change it? That's the point we're going to talk about today.

Heap memory and stack memory in JS

There are two main types of variables stored in the JS engine, heap memory and stack memory .

Similar to memory processing in Java, stack memory is primarily used to store variables of various basic types , including Boolean, number, String, Undefined, null,**, and object variable pointers. This time the stack memory gives a feeling like a linear arrangement of space, each small unit size is basically equal.

While heap memory is primarily responsible for storage of variable types such as Object objects, such as

The variables in the stack memory are generally known or scoped, and are counted as a simple store. The object type data stored in the heap memory is generally unknown in terms of size. Personally, this is why NULL is stored in the stack memory as a variable of type object.

So when we define a const object, we say that the constant is a pointer, that is, the const object corresponding to the heap memory point is constant, but the heap in the memory of the data itself or the size of the property is variable. For the base variable defined by the const, this value is equivalent to the Const object's pointer, which is immutable.

Now that you know the const in memory storage, then the const, let defined variables can not be defined two times the process is easier to guess, each time the use of const or let to initialize a variable, will first traverse the current memory stack, see if there are no duplicate variables, some will return an error.

Speaking of which, there is a very easy to ignore the point, but also did not notice that you have been initialized with the new keyword is not stored in the stack memory. Why is it? New we all know that the generation of an object , not the base type, is generated based on the constructor. Look at one more example.

var a = new String('123')var b = String('123')var c = '123'console.log(a==b, a===b, b==c, b===c, a==c, a===c)  >>> true false true true true falseconsole.log(typeof a)>>> 'object'

We can see the new string, which is the object, and the direct literal assignment and the factory pattern are all strings. But according to our analysis of the size of the relatively fixed can be expected even if the object can be stored in the stack memory, such as NULL, why this is not it? Just keep looking.

var a = new String('123')var b = new String('123')console.log(a==b, a===b)>>> false false

Obviously, if a, b is stored in the stack memory, the two should be significantly equal, just as null = = = NULL is true, but the result is not equal, indicating that both are stored in heap memory, the pointer pointing to the inconsistency.

Here, think about it. The value types and reference types that we often say are actually stack memory variables and heap memory variables, and think of value passing and reference passing, deep copy, and shallow copy, all around stack memory, one for processing values and one for processing pointers.

memory allocation and garbage collection

In general, stack memory linear sequential storage, small capacity, system allocation efficiency is high. The heap memory is the first to allocate the storage area in the heap memory, and then to store the pointer in the stack memory, the efficiency is relatively lower.
Garbage collection, the stack memory variables are basically recycled, and push in memory variables because there are many uncertain references, only when all the called variables are destroyed before recycling.

Continue to think, there are a lot of things to learn, today first come here, follow up to add.

Say ~nan will also be stored in the heap memory it? Everyone think about it, welcome to discuss the discussion ~ The text if there is a mistake welcome to say ~

Analysis of heap memory and stack memory in JS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.