"Notes" 188 recommendations for improving JavaScript programs 27-33

Source: Internet
Author: User

27. Be careful if hidden bugs


(1) if (A = 1) {...}
The comparison operator is wrongly written as an assignment operator.
To prevent such low-level annoying errors, it is recommended that in the comparison operation of the conditional expression, the constants are written on the left side and the variables written on the right. So when you write wrong, will error.
if (1 = = a) {...}


(2) a semicolon (;) is added after the IF
if (a==1); {
...
}




28, using the table-checking method to improve the performance of condition detection
When there are a large number of discrete values that need to be tested, using if and switch is much slower than using the look-up table method.
Like what:

Switch (value): Case 0:return result0;case 1:return result1;case 2:return result2;case 3:return result3;case 4:retu  RN result4;case 5:return result5;case 6:return result6;case 7:return result7;case 8:return result8;case 9:return Result9;default:return result10;}


Switch
var results = [Result0,result1,result2,result3,result4,result5,result6,result7,result8,result9,result10];return Results[value];




When using the table-checking method, all condition judgments must be completely eliminated. The operation is converted into an array item query or an object member query. One of the main advantages of using the look-up table method is that, because there is no conditional judgment, the additional performance overhead is essentially not increased when the number of candidate values increases.




29. Accurate use of the circulating body
Of the four loop types provided by JS, only the in loop performs significantly slower than the other loops.
Avoid using a for in loop unless you need to manipulate an unspecified number of object properties.




30. Using recursive mode
Recursive functions are limited by the size of the browser call stack.
When too many recursion is used and the maximum call stack size is exceeded, the browser pops up an error message.
Try{recurse ();} catch (ex) {alert (' Error info ');}


When there is a problem with the call stack size limit, the first step is positioned on the recursive instance in the code.
Two types of recursion:
(1) Direct recursion:
function R () {R ();} R ();




(2) Compact mode:
function A () {B ();} Function B () {A ();}




A ();


A common stack overflow reason is an incorrect termination condition. If the condition is correct, then the algorithm contains too many levels of recursion, and in order to be safe to run in the browser, you should use iteration, tabulation, or blending mode instead.




31. Using iterations
Using iterations instead of recursion can slow down, but avoids stack overflow errors.


32. Using watchmaking (memory recursion)
For example, factorial can be changed to:
function menfactorial (n) {if (!memfactorial.cache) {Memfactorial.cache = {' 0 ': 1, ' 1 ': 1};} if (!menfactorial.cache.hasownproperty (n)) {Memfactorial.cache[n] = n*memfactorial (n-1);} return memfactorial.cache[n];}




The key to designing factorial functions using watchmaking is the resume a cache object, which is located inside the function, with two of the simplest factorial pre-built, and before calculating the factorial, check that the cache already has the corresponding calculation results. There is no corresponding buffer value indicating that this is the first time the factorial of this value is calculated, and after the calculation is completed, the results are stored in the cache for later use.




33, optimize the cycle structure
(1) Optimize the structure
(2) Avoid unnecessary duplication of operations
Things that do not need to be changed, such as the declaration of an array, can be placed outside the loop body without declaring it every time.
(3) Proper definition of cyclic variables




"Notes" 188 recommendations for improving JavaScript programs 27-33

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.