In-depth understanding of JavaScript memory allocation

Source: Internet
Author: User

The variables in JavaScript are divided into two types, primitive values and reference values. The original value refers to the value of the original data type, such as the value represented by the Undefined,null,number,string,boolean type. A reference value refers to a value of a composite data type, Object,function,array, and so on.

The original value and the reference value are stored in memory in the stack and heap , respectively. The original values are simple data segments stored in the stack whose values are stored directly in the variable access location. reference values are objects that are stored in the heap .

The value stored in the stack is a pointer to the actual object stored in the heap.

Let's look at a piece of code:

function Person (id,name,age) {this.id = id, this.name = name; this.age = age;} var num = 10; var Bol = true; var str = "ABC"; var obj = new Object (); var arr = [' A ', ' B ', ' C ']; var person = new Person ("JXL", 22);

Then look at the memory Analysis diagram:


We cannot manipulate the data stored in the heap directly, but we can manipulate the object through references in the stack. So why separate? Aren't they all stored in memory?

The advantage of the stack is that the access speed is faster than the heap, second only to the registers directly in the CPU, but the disadvantage is that the data size and lifetime in the stack must be deterministic and inflexible. The advantage of the heap is that the memory can be allocated dynamically, the lifetime does not have to tell the compiler beforehand, the garbage collector automatically collects the data that is no longer used, but the disadvantage is that the memory is allocated dynamically at runtime, so the access speed is slower.

Therefore, compared to the simple data types, they occupy less memory, if placed in the heap, the lookup will waste a lot of time, and the heap of data into the stack will also affect the efficiency of the stack. For example, objects and arrays can be infinitely expanded, just in a heap that can be dynamically allocated size.



In-depth understanding of JavaScript memory allocation

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.