Share a few JavaScript performance tuning points

Source: Internet
Author: User
Tags eval

Javascript's performance tuning point

A, use caution to remember that the "eval" code is 100 times times slower than the "eval" code. The main reasons are: JavaScript code performs a "precompiled" operation before execution: first, it creates an active object in the current execution environment and sets those variables declared with Var to the properties of the active object, but at this point the variables are assigned to undefined, and those values are Function-defined functions are also added as properties of the active object, and their value is precisely the definition of the function. However, if you use "eval", the Code in eval (which is actually a string) cannot be identified in advance and cannot be resolved and optimized in advance, that is, the precompiled operation cannot be performed. Therefore, its performance will also be greatly reduced.

b, the recommended use of local variables JavaScript code to interpret execution, in the inside of the function, it will analyze the current variables, and the variables are grouped into different levels (level), in general:
Local variables are placed at level 1 (shallow) and global variables are placed at Level 2 (deep). If you enter a "with" or "Try–catch" code block, the new hierarchy is added, the variables in "with" or "catch" are placed in the shallow layer (layer 1), and the previous levels are deepened in turn. The lighter the layer in which the variable is located, the faster the access (read or modify) is, especially for functions that use global variables in large numbers.

C, string array method of stitching to avoid the overhead of IE6

The code is as follows Copy Code
var tips = ' tip1 ' + ' tip2 ';

This is our common way of stitching strings, but this way there will be some temporary variable creation and destruction, affecting performance, especially under IE6, so it is recommended to use the following way stitching:

The code is as follows Copy Code

var tip_array = [],tips;  Tip_array.push (' tip1 ');  Tip_array.push (' tip2 '); Tips = Tip_array.join (');

Of course, the latest browsers (such as Firefox firefox3+,ie8+, and so on) have optimized the concatenation of strings, and performance is slightly faster than the "join" method of the array.

The above list only three common optimization methods, only throwing bricks to lure jade, more JavaScript optimization points, such as avoiding implicit type conversion, narrowing the object access level, using variable optimization string matching and so we can continue to dig deeply;

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.