[·Òë]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
Ïà¹ØÎĵµ£º
±¾ÎÄÖ÷ÒªÊdzöÓÚÓÐÅóÓÑʹÓÃÎÒÔÀ´Ð´µÄautocompleteµÄJS¿Ø¼þ¡£µ±Êý¾ÝÁ¿´óµÄʱºò£¬»á³öÏÖЧÂʼ«ÆäÂýµÄÇé¿ö£¬ÎÒÔÚÕâ¶Îʱ¼ä×ö³öµÄһЩ²âÊÔÒ²¼°Ò»Ð©¾Ñ飬Óë´ó¼Ò·ÖÏí£¬Èç¹ûÓдíµÄµØ·½£¬»¹ÇëÖ¸³ö¡£
¾¹ý²âÊÔ£¬ÎÒÃǻᷢÏÖÈçϵÄÇé¿ö»òÕß˵µÄ½áÂÛ£¬Èç¹ûÄúµÄ²âÊÔ½á¹ûÓëÎҵIJ»·û£¬Çë˵Ã÷ÔÒò£¬ÒÔ±ãÏ໥ѧϰ¡£
1£©µ±Ò»¸ö½Ï´óµÄHTML×Ö· ......
JavaScriptʼþ´óÈ«
click() ¶ÔÏó.click() ʹ¶ÔÏó±»µã»÷¡£
closed ¶ÔÏó.closed ¶ÔÏó´°¿ÚÊÇ·ñÒѹرÕtrue/false
clearTimeout(¶ÔÏó) Çå³ýÒÑÉèÖõÄsetTimeout¶ÔÏó
clearInterval(¶ÔÏó) Çå³ýÒÑÉèÖõÄsetInterval¶ÔÏó
confirm("ÌáʾÐÅÏ¢") µ¯³öÈ·ÈÏ¿ò£¬È·¶¨·µ»ØtrueÈ¡Ïû·µ»Øfalse
cursor:Ñùʽ ¸ü¸ÄÊó±êÑùÊ ......
3¡¢×éºÏ¹¹Ô캯Êý/ÔÐÍ·½Ê½Ð´À࣬²ÉÓÃÇ°ÃæÖÖ·½Ê½¼Ì³Ð
ÕâÖÖ·½Ê½¸¸À࣬×ÓÀàµÄÊôÐÔ¶¼¹ÒÔÚ¹¹Ô캯ÊýÀ·½·¨¶¼¹ÒÔÚÔÐÍÉÏ¡£
/**
* ¸¸ÀàPolygon:¶à±ßÐÎ
*/
function Polygon(sides) {
this.sides = sides;
}
Polygon.prototype.setSides = function(s) {this.sides=s;}
/**
* Triangle Èý½ÇÐÎ
* @param {Object} b ......
Repaints and Reflows ÖØ»æºÍÖØÅŰæ
Once the browser has downloaded all the components of a page—HTML markup, JavaScript, CSS, images—it parses through the files and creates two internal data structures:
µ±ä¯ÀÀÆ÷ÏÂÔØÍêËùÓÐÒ³ÃæHTML±ê¼Ç£¬JavaScri ......
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 ......