Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between constant pools and heaps in javascript

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

This article introduces the relevant knowledge of "what is the difference between constant pool and heap in javascript". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

The difference between constant pool and heap in javascript: 1, constant pool is used to store constant and basic data types, while heap is used to store complex data types; 2, constant pool is more efficient, while heap needs to allocate space and addresses, and store addresses in the stack, so the efficiency is lower than that of stack (constant pool).

The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.

The memory of javascript is divided into stack memory, heap memory, constant pool, and constant pool are generally summed up in the stack.

Stack memory: used to store basic data types

Constant pool: used to store constant basic data types (generally classified as stack memory)

Heap memory: used to store complex data types

When the basic data type of a variable is stored, the value of the variable is stored in stack memory

When a variable stores a complex data type, the value of the variable is a memory address assigned by js that points to the complex data type in the heap memory

Stack memory (constant pool) because of its characteristics, so its system efficiency is high. Heap memory needs to allocate space and addresses, and the addresses are stored in the stack, so it is less efficient than the stack.

Stack memory

Because the base number type takes up less space, has a fixed size, and is frequently used, it is saved in stack memory.

Take Number, one of the basic data types, as an example:

When we declare a basic data type variable, we store the variable name and specific value in stack memory

When we use console.log (a), we output the corresponding value

Heap memory

Because complex data types take up a lot of space, the size is not fixed, and the performance is affected in the stack, so it is stored in heap memory.

When we declare a variable to store a complex data type, we generate a variable name and a specific value in stack memory, and this specific value is a memory reference address allocated by JS, which points to the complex data type we created in heap memory. When we call the variable, the parser references the corresponding object according to the reference address of the variable.

Replication of variables

Replication of basic data types:

Var num1=5;var num2=num1

In the above code, num1 and num2 are actually two different variables, and their operations are not related to each other, because in js, this kind of variable replication is actually generating a copy of the a variable (that is, num2=1).

As shown in the picture

Replication of complex data types:

Var person= {name:' Li Hua'}; / / create an object var per=person; / / copy the object

Unlike the replication of basic data type variables, when we copy a complex data type object, its essence is to copy the memory reference address of the variable, so the person and per reference addresses are the same, they both refer to the same object, whether they are operating person or per, they are all operating on the same object in memory.

As shown in the figure:

The transfer of function parameters is actually a kind of variable copy.

That's all for "what's the difference between a constant pool and a heap in javascript?" for the content of var xchang1: var function fun (x) {constant pool 1;}. Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report