foreword
Hello everyone, I am a forest three hearts, words of the most difficult point of knowledge with the most user-friendly is my motto, is based on the premise advanced is the beginning of my heart.
In our cognition: basic type storage memory, reference data type storage memory.
const a = '林三心'
const b = {
age: 18,
height: 180
}
super long string
Everyone knows that the string belongs to the
basic type, so everyone will think that the
string is stored in the
stack memory, but everyone should know that the V8 default stack memory is
984Kib
, then if a super long string > 984Kib Installed into
stack memory? This is also a relatively classic problem - Elephant boxing problem ask: Can an elephant fit into a small box?
check it out
check it out
heap snapshot
heap snapshot
First look at a piece of code
const func = function() {
this.str1 = '林三心'
this.str2 = 'Sunshine_Lin'
}
const a = new func()
const b = new func()
Then let's take a look at the details of the
heap snapshot
The above results can be seen:
Both a and b's
str1
point to the same addressBoth a and b's
str2
point to the same address
Then can we guess a conclusion: string is stored in the heap memory, the pointer is stored in the stack memory, and the same string points to the same heap memory address
Modify and add strings
Modify and add strings
Let's modify the code slightly
const func = function() {
this.str1 = '林三心'
this.str2 = 'Sunshine_Lin'
}
const a = new func()
const b = new func()
// 修改str1
a.str1 = '哈哈哈哈哈哈哈哈哈哈'
// 新增str3,跟str2一样
a.str3 = 'Sunshine_Lin'
Let's take a look at the details of the
heap snapshot
The above results can be seen:
str1
modified into a new string, a new memory space (new address) is re-openedstr3
added, the pointer points to the existing memory spaceSunshine_Lin
Then can we guess a conclusion: adding or modifying a string, if it is a string that did not exist before, it will create a new memory space, if it is existing, then directly use the existing memory space
Source code analysis
Source code analysis
When we declare a string:
1, v8 internal called
stringTable
ofhashmap
cache all the strings in the V8 reading our code when converting abstract syntax tree, confronted with a string, according to its characteristics in terms of ahash value, inserted into
hashmap
. Afterwards, if a string with the same hash valueis encountered, it will be taken out of it first for comparison. If it is consistent, a new string class will not be generated.
2. When caching strings, different
hash
methods are adopted according to different strings.
source code
source code
Easy to understand summary
Easy to understand summary
When we create a new string, V8 will check from memory to see if there is already the same string, and reuse it directly if found. If it is not found, open up a new memory space to store the string and assign the address to the variable.
Have you ever wondered why strings cannot be modified by subscript indexing? Because the modification of the string can only be done through the entire modification in essence, and cannot be modified locally.
Epilogue
Epilogue
I'm Lin Sanxin, an enthusiastic front-end rookie programmer. If you are motivated, like the front-end, and want to learn the front-end, then we can make friends and fish together haha, touch the fish group, add me, please note [Si No]
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。