Home > Web Front-end > JS Tutorial > body text

JavaScript enhancement tutorial-javascript performance optimization

黄舟
Release: 2017-01-21 16:06:52
Original
1141 people have browsed it

The access location of data also affects the running speed of JavaScript. Let’s summarize the most reasonable way to allocate access locations and run JavaScript efficiently.
The data access locations are divided into 1. Literals (such as true , false, null, etc.) 2. Local variables (var a=5;) 3. Array elements (var arr=[]; arr[0]=5;) 4. Object members
All of the above can store data
Based on the time required to read data 200,000 times by different browsers, the order from fast to slow is 1 2 3 4 (comprehensive results, different browsers will have fluctuations)

Fast and efficient execution JavaScript code summary:
1. Accessing literals and local variables is faster than array elements and object members.
2. Since local variables are at the beginning of the scope chain, accessing local variables is faster than accessing cross-scope variables. The deeper the variable is in the scope chain, the slower the access speed. Since global variables are always at the end of the scope chain, the access speed is the slowest.
3. Nested object members will obviously affect performance, so use them as little as possible.
4. The deeper the property or method is in the prototype chain, the slower the access speed.
5. Generally speaking, you can save array elements, object members, and cross-domain variables in local variables to improve JavaScript performance, because accessing local variables is faster
6. Use with as little as possible, because it will Changing the execution environment scope chain also has the same effect as the catch substatement in the type-catch statement, so be careful.
Through the above distribution, you can significantly improve the actual performance of web applications that use a large number of JavaScript.

The above is the content of the JavaScript enhancement tutorial-javascript performance optimization. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!