[·Òë]High Performance JavaScript(014)
Recursion Patterns µÝ¹éģʽ
When you run into a call stack size limit, your first step should be to identify any instances of recursion in the code. To that end, there are two recursive patterns to be aware of. The first is the straightforward recursive pattern represented in the factorial() function shown earlier, when a function calls itself. The general pattern is as follows:
µ±ÄãÏÝÈëµ÷ÓÃÕ»³ß´çÏÞÖÆʱ£¬µÚÒ»²½Ó¦¸Ã¶¨Î»ÔÚ´úÂëÖеĵݹéʵÀýÉÏ¡£Îª´Ë£¬ÓÐÁ½¸öµÝ¹éģʽֵµÃ×¢Òâ¡£Ê×ÏÈÊÇÖ±½ÓµÝ¹éģʽΪ´ú±íµÄÇ°ÃæÌáµ½µÄfactorial()º¯Êý£¬¼´Ò»¸öº¯Êýµ÷ÓÃ×ÔÉí¡£ÆäÒ»°ãģʽÈçÏ£º
function recurse(){
recurse();
}
recurse();
This pattern is typically easy to identify when errors occur. A second, subtler pattern involves two functions:
µ±·¢Éú´íÎóʱ£¬ÕâÖÖģʽ±È½ÏÈÝÒ׶¨Î»¡£ÁíÍâÒ»ÖÖģʽ³ÆΪ¾«ÇÉģʽ£¬Ëü°üº¬Á½¸öº¯Êý£º
function first(){
second();
}
function second(){
first();
}
first();
In this recursion pattern, two functions each call the other, such that an infinite loop is formed. This is the more troubling pattern and a far more difficult one to identify in large code bases.
ÔÚÕâÖֵݹéģʽÖУ¬Á½¸öº¯Êý»¥Ïàµ÷ÓöԷ½£¬ÐγÉÒ»¸öÎÞÏÞÑ»·¡£ÕâÊÇÒ»¸öÁîÈ˲»°²µÄģʽ£¬ÔÚ´óÐÍ´úÂë¿âÖж¨Î»´íÎóºÜÀ§ÄÑ¡£
Most call stack errors are related to one of these two recursion patterns. A frequent cause of stack overflow is an incorrect terminal condition, so the first step after identifying the pattern is to validate the terminal condition. If the terminal condition is correct, then the algorithm contains too much recursion to safely be run in the browser and should be changed to use iteration, memoization, or both.
´ó¶àÊýµ÷ÓÃÕ»´íÎóÓëÕâÁ½ÖÖģʽ֮һÓйء£³£¼ûµÄÕ»Òç³öÔÒòÊÇÒ»¸ö²»ÕýÈ·µÄÖÕÖ¹Ìõ¼þ£¬ËùÒÔ¶¨Î»Ä£Ê½´íÎóµÄµÚÒ»²½ÊÇÑéÖ¤ÖÕÖ¹Ìõ¼þ¡£Èç¹ûÖÕÖ¹Ìõ¼þÊÇÕýÈ·µÄ£¬ÄÇôËã·¨°üº¬ÁËÌ«¶à²ãµÝ¹é£¬ÎªÁËÄܹ»°²È«µØÔÚä¯ÀÀÆ÷ÖÐÔËÐУ¬Ó¦µ±¸ÄÓõü´ú£¬ÖÆ
Ïà¹ØÎĵµ£º
±¾ÎÄÖ÷ÒªÊdzöÓÚÓÐÅóÓÑʹÓÃÎÒÔÀ´Ð´µÄautocompleteµÄJS¿Ø¼þ¡£µ±Êý¾ÝÁ¿´óµÄʱºò£¬»á³öÏÖЧÂʼ«ÆäÂýµÄÇé¿ö£¬ÎÒÔÚÕâ¶Îʱ¼ä×ö³öµÄһЩ²âÊÔÒ²¼°Ò»Ð©¾Ñ飬Óë´ó¼Ò·ÖÏí£¬Èç¹ûÓдíµÄµØ·½£¬»¹ÇëÖ¸³ö¡£
¾¹ý²âÊÔ£¬ÎÒÃǻᷢÏÖÈçϵÄÇé¿ö»òÕß˵µÄ½áÂÛ£¬Èç¹ûÄúµÄ²âÊÔ½á¹ûÓëÎҵIJ»·û£¬Çë˵Ã÷ÔÒò£¬ÒÔ±ãÏ໥ѧϰ¡£
1£©µ±Ò»¸ö½Ï´óµÄHTML×Ö· ......
C#´úÂëÓëjavaScriptº¯ÊýµÄÏ໥µ÷ÓÃ
ÎÊ£º
1.ÈçºÎÔÚJavaScript·ÃÎÊC#º¯Êý?
2.ÈçºÎÔÚJavaScript·ÃÎÊC#±äÁ¿?
3.ÈçºÎÔÚC#ÖзÃÎÊJavaScriptµÄÒÑÓбäÁ¿?
4.ÈçºÎÔÚC#ÖзÃÎÊJavaScriptº¯Êý?
ÎÊÌâ1´ð°¸ÈçÏ£º
javaScriptº¯ÊýÖÐÖ´ÐÐC#´úÂëÖеĺ¯Êý£º
·½·¨Ò»£º1¡¢Ê×ÏȽ¨Á¢Ò»¸ö°´Å¥£¬ÔÚºǫ́½«µ÷Óûò´¦ÀíµÄÄÚÈÝдÈëbutton_clickÖÐ ......
var
xmlDoc
=
null
;
function
parseXML
(
xmlUrl
)
{
¡¡¡¡try
{
¡¡¡¡¡¡¡¡//IE
¡¡¡¡¡¡¡¡xmlDoc
=
new
ActiveXObject
(
"Microsoft.XMLDOM"
);
¡¡¡¡¡¡¡¡xmlDoc
.
async
=
false
;
¡¡¡¡¡¡¡¡xmlDoc
......
µÚËÄÕ Algorithms and Flow Control Ëã·¨ºÍÁ÷³Ì¿ØÖÆ
The overall structure of your code is one of the main determinants as to how fast it will execute. Having a very small amount of code doesn't necessarily mean that it will run quickly, and having a large amount of code ......
Conditionals Ìõ¼þ±í´ïʽ
Similar in nature to loops, conditionals determine how execution flows through JavaScript. The traditional argument of whether to use if-else statements or a switch statement applies to JavaScript just as it does to other languages. Since different b ......