Understanding javascript_01_ Understanding Memory Allocation "Go"

Source: Internet
Author: User
Tags object model

Before I start, I'd like to say a few words to understand that the JavaScript series is about explaining the JavaScript principle by leading you through the analysis of the memory allocations at the time of JavaScript execution, covering JavaScript preloading, closure principle, surface object, execution model , the object model ..., the perspective of the article is very special, but also very deep, I hope that we can accept this form, and provide valuable advice.

Raw values and reference values

In ECMAScript, a variable can hold two types of values, that is, the original value and the reference value.

The original value refers to the value representing the original data type (the base data type), which is the value represented by the Undefined,null,number,string,boolean type.

The reference value refers to the value of the composite data type, that is, the Object,function,array, and the custom object, and so on

Stacks and heaps

Memory-as-stack and heap with two structures corresponding to the original value and the reference value

A stack is a last-in-first-out data structure that can be used to simulate the behavior of stacks in JavaScript by using array

12345 var  arr = []; //Create a stack arr.push ( "apple" //pressing element "Apple" ["Apple"] arr.push ( ); //pressing element "Orange"    ["Apple", "orange"] arr.pop (); //pops up "Orange"       ["apple"] arr.push ( " banana " //push element "banana"    ["apple", "banana"]

Let's take a look at the corresponding memory graph:

The original values are simple data segments stored in the stack, that is, their values are stored directly in the location where the variable is accessed.

A heap is a hashing algorithm-based data structure that holds data, and in JavaScript, reference values are stored in the heap.

The reference value is the object stored in the heap, that is, the value stored at the variable (that is, the variable that points to the object, stored in the stack) is a pointer to the actual object stored in the heap.

Example: var obj = new Object (); obj is stored in the stack and it points to the new object (), and the new object () is stored in the heap.

So why should the reference values be placed in the heap, and the original values put on the stack, not all in memory, why not put them together? Then let's explore the answers to the questions!

First, let's take a look at the code:

123456789101112 functionPerson(id,name,age){    this.id = id;    this.name = name;    this.age = age;}varnum = 10;varbol = true;varstr = "abc";varobj = newObject();vararr = [‘a‘,‘b‘,‘c‘];varperson = newPerson(100,"笨蛋的座右铭",25);

Then let's take a look at the memory Analysis diagram:

Variable num,bol,str are basic data types, their values are stored directly on the stack, Obj,person,arr are composite data types, their reference variables are stored in the stack, pointing to the actual objects stored in the heap.

It is known that we cannot manipulate the data in the heap directly, that is, we cannot manipulate the object directly, but we can manipulate the object by reference to the object in the stack, just as we operate the TV with the remote control machine, the difference is that the TV itself does not have a control button.

Now let's answer the question of why the reference value should be placed in the heap, and the original value should be put on the stack:

Remember a word: energy is to keep the balance, is nothing more than time to change space, space for the question of time

Heap is larger than the stack, the stack is faster than the operation of the heap, the object is a complex structure, and can be freely extended, such as: The array can be infinitely expanded, the object can freely add properties. Put them in the heap in order not to affect the efficiency of the stack. Instead, the actual objects in the heap are looked up and manipulated by reference. The simple data type is stable relative to the simple data type, and it occupies only a small amount of memory. Not putting a simple data type in the heap is because it takes time to find the actual object by referencing it to the heap, which is much larger than the cost of getting the actual value directly from the stack. So the value of the simple data type is stored directly in the stack.

Summarize:

The procedure is very simple, but it is the fundamental of all, the foundation is the most important, because the skyscraper is also a piece of brick tile building up.

Memory is the root of the program execution, understand the memory, it is equal to understand everything.

The work of painstaking efforts, encourage yourself, come on!

Reference:

JavaScript Advanced Programming

Understanding javascript_01_ Understanding Memory Allocation "Go"

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.