Ways to optimize the execution efficiency of JavaScript

Source: Internet
Author: User
Tags eval join regular expression switch case

  This article introduces some ways to optimize the execution efficiency of JavaScript, personal feeling is good, interested friends can understand the next

1, using an array join method in a lower version of the browser (such as IE6,IE7, etc.) is much more efficient than using the + number to connect (e.g. ' AAA ', ' BBB ', ' CCC '].join () is more efficient than ' AAA ' + ' BBB ' + ' CCC ');    2, array:  pop is more efficient than shift, push is more efficient than unshift. This is important for designing the binary heap structure, and it is best to place the largest or smallest element at the end of the array.     3, Digital rounding best Use shift operation:  1.1 >> 0;    4, use direct amount to create array and object:  var a = [];  var o = {};     5, object level do not nest too much, reduce object lookup:  Don't use A.B.C.D.E, this design way get E object.     6, key value corresponding to the value, and switch case comparison, the key value corresponding to the switch case efficiency, each browser has been tested, you can look at this comparison of the article JavaScript Small experiment;    7, If you use JQ, there is also a $ (' xxxx '). Empty (). Append (' xxxxxxx '); and $ (' xxxxx '). HTML (' xxxxx '); The result is $ (' xxxx '). Empty (). Append (' xxxxxxx '); win, article address is jquery small experiment;    8, loop   in JavaScript, we can use for (;;), while (), for (in) three kinds of loops, three loops in Efficiency is extremely poor, because he needs to query the hash key, as long as can be as little as possible. for (;;) The performance of the while loop should be the same as the basic (usually used) equivalent.     If the loop variable is incremented or decremented, do not assign a value to the loop variable alone, and you should use the nested + + or-operator for the last time it reads.     If you want to compare the length of an array, you should first put the array's length property into a local variable, reducing the number of queries.     9, local variables, and global variables   Local variables are faster than global variables, because global variables are actually members of global objects, and local variables are placed in the stack of functions.   &NBSP 10. Using eval without using eval  is equivalent to calling the interpretation engine to run the content again at run time, consuming a lot of time. This time using JavaScript-supported closures enables functional templates (refer to the content of functional programming for closures);    11, string concatenation   If it is an append string, it is best to use the s+=anotherstr operation. Instead of using s=s+anotherstr;    12, converting numbers to strings, applying "" + 1, although it looks uglier, it's actually the highest efficiency, and performance:    ("" +) > string () >. toString () > New String ()     This is actually a bit like the "direct volume" below, where you can use the internal operations you use at compile time faster than the user actions that you use at run time.     String () is an intrinsic function, so it's very fast, and. ToString () to query the functions in the prototype, so the speed is less, the new String () is used to return an exact copy;    13, Floating-point numbers are converted to integers, which is more prone to error, and many people prefer to use parseint (), in fact parseint () is used to convert strings to numbers rather than floating-point and integer conversions, and we should use Math.floor () or Math.Round ().     14, string traversal operation   The string loop operation, such as substitution, lookup, should use regular expression, because the JavaScript itself is slow cycle, and regular expression of the operation is written in C language API, performance is very good;     15, timer   If it is for the running code, should not use settimeout, but should be used setinterval. SetTimeout each time you want to reset a timer;    update ..., please wait!    
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.