Implement high-performance data storage in JavaScript
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中 }
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; } }
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)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



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 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 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

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

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

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

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

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
