This is the 15th day of my participation in the August Text Challenge.More challenges in August
There are two main storage locations for variables in THE JS engine, heap memory and stack memory. These two work cooperatively to store the two data types in JS: basic data type and complex data type respectively
First, data types
To learn about stacks, let’s take a look at ECMAScript data types. Data types fall into two basic types: complex data types:
- Basic types: Number, String, Boolean, null, undefined, Symbol;
- Complex types: Object (Object, Array, function, Date, RegExp).
We believe that these two types of data are too common, the development of a random let a variable is one of the two types, so we declare variables after how to store?
There are two main storage locations for variables in JS engine: heap memory and stack memory. Different types of data are stored in different ways.
- Basic types: Values of basic types occupy a fixed size in memory and are stored in stack memory
- Reference type: The value of a reference type is an object and is stored in heap memory, which in turn stores the variable identifier of the object and the storage address of the object in heap memory
I will introduce two diagrams to help you understand the above two sentences
-
Basic data type storage mode
let a = 1; let b = true; let c = null; Copy the code
When I declare variables of the three basic data types, the stack memory stores variables in the same way as shown in the following figure. The basic data types take up small space, are fixed in size, and are accessed by value.
-
Storage mode for complex data types
Let obj = {}; let arr = []; let map = new Map();Copy the code
When I declare the above three complex data types, stack, heap memory storage variable ways like shown below, in the stack memory access is the address of the reference data entity, the application address can be found in the heap memory corresponding entity data, an interpreter for complex data types, you will need to get the stack in the address, after the address, then the entity data obtained from the heap
The stack memory here stores the reference address of the replicated data type, and the corresponding complex data type can be obtained from this reference address in the heap memory