Home > Web Front-end > JS Tutorial > Clever use of local variables to improve javascript performance_javascript tips

Clever use of local variables to improve javascript performance_javascript tips

WBOY
Release: 2016-05-16 16:58:21
Original
1146 people have browsed it

The deeper an identifier is located in JavaScript, the slower it is to read and write. Therefore, reading and writing local variables in a function is always the fastest, while reading and writing global variables is usually the slowest. A good rule of thumb is: if a cross-scope value is referenced more than once in a function, store it in a local variable.

For example:

Copy code The code is as follows:




This function references document three times, and document is a global object. The process of searching for this variable must traverse the entire scope chain until it is finally found in the global variable object. You can reduce the impact on performance by first storing a reference to a global variable in a local variable, and then using this local variable instead of the global variable.

For example:
Copy code The code is as follows:



source: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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template