## preface

Hello everyone, I am Lin Sanxin, with the most easy to understand the most difficult knowledge points is my motto, the basis is advanced premise is my initial mind.

In our understanding: base types are stored in stack memory, reference data types are stored in heap memory.

``````const a = 'Lin Three Hearts'
const b = {
age: 18.height: 180
}
Copy the code``````

## Superlong string

As you know, strings are basic types, so you might think that strings are stored in stack memory, but remember, V8’s default stack memory is 984Kib, so if an extremely long string > 984Kib can fit into the stack memory? This is also a more classic problem – elephant packing problem, try to ask: can an elephant fit into a small box?

## To find out

### Heap snapshot

``````const func = function() {
this.str1 = 'Lin Three Hearts'
this.str2 = 'Sunshine_Lin'
}

const a = new func()
const b = new func()
Copy the code``````

Then let’s look at the heap snapshot in detail

The above results can be seen:

• A and b`str1`They all point to the same address
• A and b`str2`They all point to the same address

The contents of the string are stored in the heap, the Pointers are stored in the stack, and the same string points to the same heap address

Let’s change the code a little bit

``````const func = function() {
this.str1 = 'Lin Three Hearts'
this.str2 = 'Sunshine_Lin'
}

const a = new func()
const b = new func()

/ / modify str1
a.str1 = 'Hahahahahahahahahaha'
// Add str3, same as STR2
a.str3 = 'Sunshine_Lin'
Copy the code``````

Let’s take a look at the heap snapshot details at this stage

The above results can be seen:

• `str1`After changing to a new string, a new memory space is opened (new address)
• `str3`After the addition, the pointer points to the existing one`Sunshine_Lin`Memory space

Can we guess a conclusion: after adding or modifying a string, if it is a string that does not exist before, it will create a new memory space. If it is an existing string, it will directly use the existing memory space

### Source code analysis

When we declare a string:

• 1. V8 has an internal name called`stringTable`the`hashmap`All strings are cached, and when V8 reads our code and transforms the abstract syntax tree, every string it encounters is converted to one based on its characteristics`Hash value`That is inserted into the`hashmap`In the. In the future if encountered`Hash value`If the string is consistent, it will be taken from it first for comparison. If it is consistent, no new string class will be generated.
• 2. When caching strings, take different values according to different strings`hash`Way.

### Easy to understand summary

String data is stored in heap memory, and only its address pointer is stored in stack memory

When we create a new string, V8 looks in memory to see if the same string already exists and reuses it. If not, a new memory space is created to hold the string and the address is assigned to the variable.

Have you ever wondered why strings can’t be modified by subscript indexes? Because string changes can only be made through whole changes, not locally.

## Refer to the article

• Are JS variables stored in a heap or on a stack? (Deep into memory principle)

## conclusion

I am Lin Sanxin, an enthusiastic front-end novice programmer. If you progress, like the front end, want to learn the front end, then we can make friends, touch fish ha ha, touch fish, point this –> touch fish boiling point