[·Òë]High Performance JavaScript(016)
Regular Expression Optimization ÕýÔò±í´ïʽÓÅ»¯
Incautiously crafted regexes can be a major performance bottleneck (the upcoming section, "Runaway Backtracking" on page 91, contains several examples showing how severe this can be), but there is a lot you can do to improve regex efficiency. Just because two regexes match the same text doesn't mean they do so at the same speed.
´ÖdzµØ±àдÕýÔò±í´ïʽÊÇÔì³ÉÐÔÄÜÆ¿¾±µÄÖ÷ÒªÔÒò£¨ºóÃæ“»ØËÝʧ¿Ø”Ò»½ÚÓÐһЩÀý×Ó˵Ã÷ÕâÊǶàôÑÏÖØµÄÎÊÌ⣩£¬µ«»¹Óкܶà¿ÉÒԸĽøÕýÔò±í´ïʽЧÂʵĵط½¡£Á½¸öÕýÔò±í´ïʽƥÅäÏàͬµÄÎı¾²¢²»Òâζ×ÅËûÃǾßÓÐͬµÈµÄËÙ¶È¡£
Many factors affect a regex's efficiency. For starters, the text a regex is applied to makes a big difference because regexes spend more time on partial matches than obvious nonmatches. Each browser's regex engine also has different internal optimizations.
Ðí¶àÒòËØÓ°ÏìÕýÔò±í´ïʽµÄЧÂÊ¡£Ê×ÏÈ£¬ÕýÔò±í´ïʽÊÊÅäµÄÎı¾Ç§²îÍò±ð£¬²¿·ÖÆ¥Åäʱ±ÈÍêÈ«²»Æ¥ÅäËùÓõÄʱ¼äÒª³¤¡£Ã¿ÖÖä¯ÀÀÆ÷µÄÕýÔò±í´ïʽÒýÇæÒ²Óв»Í¬µÄÄÚ²¿ÓÅ»¯¡£
Regex optimization is a fairly broad and nuanced topic. There's only so much that can be covered in this section, but what's included should put you well on your way to understanding the kinds of issues that affect regex performance and mastering the art of crafting efficient regexes.
ÕýÔò±í´ïʽµÄÓÅ»¯ÊÇÒ»¸öÏ൱¹ã·ººÍϸÖÂÈë΢µÄ»°Ìâ¡£±¾½ÚÌÖÂÛ¾¡ÆäËùÄÜ£¬Ï£ÍûÕâЩÄÚÈÝÓÐÖúÓÚÄúÀí½âÓ°ÏìÕýÔò±í´ïʽÐÔÄܵĸ÷ÖÖÎÊÌâºÍÕÆÎÕ±àд¸ßЧÕýÔò±í´ïʽµÄÒÕÊõ¡£
Note that this section assumes you already have some experience with regular expressions and are primarily interested in how to make them faster. If you're new to regular expressions or need to brush up on the basics, numerous resources are available on the Web and in print. Regular Expressions Cookbook (O'Reilly) by Jan Goyvaerts and Steven Levithan (that's me!) is written for people who like to learn by doing, and
Ïà¹ØÎĵµ£º
ÃæÏò¶ÔÏóµÄÓïÑÔ¶àÊý¶¼Ö§³Ö¼Ì³Ð£¬¼Ì³Ð×îÖØÒªµÄÓŵã¾ÍÊÇ´úÂ븴Ó㬴Ӷø¹¹½¨´óÐÍÈí¼þϵͳ¡£Èç¹ûÒ»¸öÀàÄܹ»ÖØÓÃÁíÒ»¸öÀàµÄÊôÐԺͻò·½·¨£¬¾Í³ÆÖ®Îª¼Ì³Ð¡£
´ÓÕâ¸ö½Ç¶ÈÀ´¿´¿´jsµÄ¼Ì³Ð·½Ê½¡£jsÖм̳з½Ê½ÓëдÀ෽ʽϢϢÏà¹Ø¡£²»Í¬µÄдÀ෽ʽÔì³É²»Í¬µÄ¼Ì³Ð·½Ê½¡£¸÷ÖÖÁ÷ÐÐjs¿â¼Ì³Ð·½Ê½Ò²¸÷²»Ïàͬ¡£´Ó×î¼òµ¥µÄ
¸´ÓÿªÊ¼¡£
1¡¢¹¹Ôìº ......
£¼script language="javaScript"£¾
function closeWindow()
{
¡¡window.opener = null;
¡¡window.open(' ', '_self', ' ');
¡¡window.close();
}
£¼/script£¾
£¼input type='button' value='¹Ø±Õ´°¿Ú' onClick="closeWindow()"£¾
»ò
£¼input type="button" value="¹ ......
µÚËÄÕ 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 ......
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 ......