JavaScript data types:

Value types: String, Number, Boolean, Null, Undefined, Symbol.

Reference data types: Object, Array, Function, Map

The stack and heap

Stack: The stack will automatically allocate memory space, will automatically free, to store basic types, simple data segments, occupy a fixed amount of space. Basic types: String, Number, Boolean, Null, Undefined,Symbol

Heap: dynamically allocated memory, not automatically released or of any size, for reference types, objects that may be composed of multiple values, stored in heap memory, containing variables of reference types, which actually hold Pointers to the object rather than the variable itself. Reference types: Function, Array, Object

The difference between

  • Stack: All variables defined in a method are stored in stack memory, and the method’s stack is destroyed when the method completes execution.

Advantages: Faster access than the heap, only second to the register directly located in the CPU, data can be shared; Disadvantages: The size and lifetime of data in the stack must be determined, inflexible.

  • Heap: An object in heap memory is not destroyed at the end of a method, and even after the method ends, the object may be referenced by another reference variable (parameter passing). The object is created for reuse and will be saved to the runtime data area.

Stack and heap overflow

  • Stack: Methods can be called recursively, so that the JVM maintains a long method call trajectory as the stack depth increases, resulting in stack overruns when memory is not allocated.
  • Heap: Creates objects in a loop, commonly known as continuously new objects.

Let’s look at the difference between value and address

  • The difference between these two types is the difference between a base type and a reference type
var a = [1.0.9.8.7];
   var b = a;
   var c = a[0];
   console.log(b);     / /,0,9,8,7 [1]
   console.log(c);     / / 1
   // Change the value
   b[1] = 3;
   c = 5;
   console.log(b[1]);  / / 3
   console.log(a[0]);  / / 1
   console.log(a);  / /,3,9,8,7 [1]
Copy the code
  • Because a is an array, it is a reference type, when assigned to b is the stack in the address, not the object in the heap memory, c is only a pile of memory access to a data value, and stored in the stack, so b changes, according to the address back to a pile of modification, c is directly modify in the stack, and can’t point to a pile of memory.

The relationship between stack and heap is roughly shown

  • Stack memory: The memory space in which the engine executes code to hold the addresses of base values and reference type values.
  • Heap memory: Used to hold an unordered set of unique reference type values, which can be retrieved using key names in the stack.

/ / basic values
var a = 1;
var b = a;
a = 2;
console.log(a); // Output: 2
console.log(b); // Output: 1
 
/ / reference value
The variables c and d refer to the same array in the heap
var c = [0.1.2];
var d = c;
c[0] = 5;
console.log(c); // Output: [5, 1, 2]
console.log(d); // Output: [5, 1, 2]
Copy the code
  • On the left is the stack, on the right is the heap

JS shallow copy (the current study basically does not touch what deep copy, will be supplemented in the future study)

  • Let’s start with a shallow copy
var a=[0.1.2.3.4];
    b=a;
console.log(a===b);  // true
    a[0] =1;
console.log(a,b);    //[1, 2, 3, 4] [1, 1, 2, 3, 4]
Copy the code
  • Basic types — names and values are stored in stack memory, for example var a=1;

When executing code b=a, a new stack memory is created, which looks like this:

If a=2 is changed, b will not be affected

  • Reference data types — the name is stored in stack memory, and the value is stored in heap memory, but the stack memory provides a reference address to the value in heap memory.
  1. First of all, the storage of array A in the computer is as follows:

When b=a is copied, the reference address of A is actually copied, instead of the value in the heap. Then a and B are stored in the computer as follows:

When a[0]=1, we modify the array, because a and B point to the same address, so naturally B is also affected, this is called shallow copy