Home Web Front-end JS Tutorial Implement high-performance data storage in JavaScript

Implement high-performance data storage in JavaScript

Feb 20, 2017 pm 02:29 PM

1. There are four basic data access locations in JavaScript: literals, local variables, array elements, and object members.

Generally speaking: [literal, local variable] running speed > [array, object member]

2. Internal properties contain a function created A collection of objects in the scope. This set is called a scope chain.

3. Execute the function->Create execution environment->Create active object (i.e. function runtime variable object).

So calling the same function multiple times will result in the creation of multiple execution environments.

4. Function execution process

Every time a variable is encountered, it will go through an identifier resolution process, where to obtain or store data. This process searches the scope chain of the execution environment. It is this search process that affects performance.

5. Performance of identifier parsing

Global variables always exist at the end of the execution environment scope. Local variables are resolved first.

Rule of thumb: If a cross-scope value is referenced more than once in a function, store it in a local variable.

For example:

function initUI(){
 var bd=document.body;
 //后面有多次doucument这个全局对象的调用
}
//->优化后
function initUI(){
 var doc=document;
  bd=doc.body;
 //把doucument这个全局对象的引用存储到局部变量doc中
 
}
Copy after login



6. Change the scope chain

Generally speaking, a The scope chain of the execution environment will not change.

<1>with can temporarily change the scope chain

width is used to create a variable for all properties of the object

function initUI(){
 with(document){
 var bd=body; 
 }
}
Copy after login



When the code is executed to with, The scope chain of the execution environment is temporarily changed. A new variable object is created, which contains all properties of the object specified by the parameter. This object is pushed into the first position of the scope chain, so at this time all local variables are in the second scope chain object, so the access cost is higher.

<2>try-catch

When an error occurs in the try statement, the execution process will automatically jump to the catch. Then push the exception object into a variable object and place it at the top of the scope.

Note: Once the catch substatement is executed, the scope chain will return to the previous state.

7. Performance issues caused by closures

Closures are one of the most powerful features of JavaScript.

Because the closure contains a reference to the same object that executes the scope chain of the environment, the active object of the function will not be destroyed, causing more memory overhead.

Performance points of concern: When cross-scope identifiers are frequently accessed, each access will cause performance loss.

Start:19:41:45 2015-11-21 Quoted from by Aaron:/content/3493261.html

8. Memory leak

Memory leak means that a piece of allocated memory cannot be used or recycled until the browser process ends. In C++, because memory is managed manually, memory leaks are a common occurrence. Nowadays, popular languages ​​​​such as C# and Java use automatic garbage collection methods to manage memory, and almost no memory leaks occur under normal use. Browsers also use automatic garbage collection to manage memory. However, due to bugs in the browser's garbage collection method, memory leaks may occur.

Several situations of memory leaks

Circular reference

Javascript closure

DOM insertion order

If a DOM object is referenced by a Javascript object and at the same time references the same or other Javascript object, the DOM object may cause a memory leak. The reference to this DOM object will not be reclaimed by the garbage collector when the script stops. To break a reference cycle, the object referencing the DOM element or a reference to the DOM object needs to be assigned null.

The specifics will be discussed in depth, here is the summary

JS memory leaks, no wonder the elements are removed from the DOM, but there are still variables or object references The DOM object. Then it cannot be deleted from the memory. This keeps the browser's memory usage high. This memory usage will be automatically released as the browser refreshes.

Another situation is circular reference. A DOM object and a JS object refer to each other. This causes a more serious situation. Even if refreshed, the memory will not be reduced. This is a memory leak in the strict sense.

The above is the content of realizing high-performance data storage in JavaScript. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Why can't localstorage successfully save data? Why can't localstorage successfully save data? Jan 03, 2024 pm 01:41 PM

Why does storing data to localstorage always fail? Need specific code examples In front-end development, we often need to store data on the browser side to improve user experience and facilitate subsequent data access. Localstorage is a technology provided by HTML5 for client-side data storage. It provides a simple way to store data and maintain data persistence after the page is refreshed or closed. However, when we use localstorage for data storage, sometimes

WebSocket and JavaScript: key technologies for implementing real-time monitoring systems WebSocket and JavaScript: key technologies for implementing real-time monitoring systems Dec 17, 2023 pm 05:30 PM

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

JavaScript and WebSocket: Building an efficient real-time weather forecasting system JavaScript and WebSocket: Building an efficient real-time weather forecasting system Dec 17, 2023 pm 05:13 PM

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

Simple JavaScript Tutorial: How to Get HTTP Status Code Simple JavaScript Tutorial: How to Get HTTP Status Code Jan 05, 2024 pm 06:08 PM

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

What type of file is a dat file? What type of file is a dat file? Feb 19, 2024 am 11:32 AM

The dat file is a universal data file format that can be used to store various types of data. dat files can contain different data forms such as text, images, audio, and video. It is widely used in many different applications and operating systems. dat files are typically binary files that store data in bytes rather than text. This means that dat files cannot be modified or their contents viewed directly through a text editor. Instead, specific software or tools are required to process and parse the data of dat files. d

How to get HTTP status code in JavaScript the easy way How to get HTTP status code in JavaScript the easy way Jan 05, 2024 pm 01:37 PM

Introduction to the method of obtaining HTTP status code in JavaScript: In front-end development, we often need to deal with the interaction with the back-end interface, and HTTP status code is a very important part of it. Understanding and obtaining HTTP status codes helps us better handle the data returned by the interface. This article will introduce how to use JavaScript to obtain HTTP status codes and provide specific code examples. 1. What is HTTP status code? HTTP status code means that when the browser initiates a request to the server, the service

2024 Huawei Data Storage New Year New Product Launch Conference will be held on February 20 2024 Huawei Data Storage New Year New Product Launch Conference will be held on February 20 Feb 12, 2024 pm 10:48 PM

According to news from this site on February 11, according to Huawei official news, the 2024 Huawei Data Storage New Year New Product Launch Conference will be held on February 20. Attached to this site is a conference introduction: Data is an important production factor in the digital economy era, a key source of value creation, and a national strategic resource. Data infrastructure plays a key supporting role in the supply, circulation and application of data elements. It is responsible for reliable storage and efficient management of data assets and their flow according to demand. As an important part of the national data strategy, data infrastructure is the cornerstone of realizing a data power. Huawei continues to innovate in the field of ICT infrastructure, develops advanced data storage capabilities, and plays a fundamental role in ensuring that data assets are “securely stored, readily available, mobile, and well used.” At the same time, Huawei insists on developing

Computer configuration recommendations for building a high-performance Python programming workstation Computer configuration recommendations for building a high-performance Python programming workstation Mar 25, 2024 pm 07:12 PM

Title: Computer configuration recommendations for building a high-performance Python programming workstation. With the widespread application of the Python language in data analysis, artificial intelligence and other fields, more and more developers and researchers have an increasing demand for building high-performance Python programming workstations. When choosing a computer configuration, in addition to performance considerations, it should also be optimized according to the characteristics of Python programming to improve programming efficiency and running speed. This article will introduce how to build a high-performance Python programming workstation and provide specific

See all articles