2
头图

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?

截屏2022-01-17 下午11.13.35.png

check it out

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

WeChat9db795005f91b874bb0097857cb6f7a3.png

The above results can be seen:

  • Both a and b's str1 point to the same address
  • Both 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

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

WeChat40be5ddc2bb160cdba3258fc98825136.png

The above results can be seen:

  • str1 modified into a new string, a new memory space (new address) is re-opened
  • str3 added, the pointer points to the existing memory space Sunshine_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

When we declare a string:

  • 1, v8 internal called stringTable of hashmap 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 a hash value, inserted into hashmap . Afterwards, if a string with the same hash value is 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.

截屏2022-01-17 下午11.13.42.png

source code

image.png

image.png

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

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]

image.png


Sunshine_Lin
2.1k 声望7.1k 粉丝