[·Òë]High Performance JavaScript(017)
A Note on Benchmarking ²âÊÔ»ù׼˵Ã÷
Because a regex's performance can be wildly different depending on the text it's applied to, there's no straightforward way to benchmark regexes against each other. For the best result, you need to benchmark your regexes on test strings of varying lengths that match, don't match, and nearly match.
ÒòΪÕýÔò±í´ïʽÐÔÄÜÒòÓ¦ÓÃÎı¾²»Í¬¶ø²úÉúºÜ´ó²îÒ죬ûÓмòµ¥Ã÷Á˵ķ½·¨¿ÉÒÔ²âÊÔÕýÔò±í´ïʽ֮¼äµÄÐÔÄܲî±ð¡£ÎªµÃµ½×îºÃµÄ½á¹û£¬ÄãÐèÒªÔÚ¸÷ÖÖ×Ö·û´®ÉϲâÊÔÄãµÄÕýÔò±í´ïʽ£¬°üÀ¨²»Í¬³¤¶È£¬Äܹ»Æ¥ÅäµÄ£¬²»ÄÜÆ¥ÅäµÄ£¬ºÍ½üËÆÆ¥ÅäµÄ¡£
That's one reason for this chapter's lengthy backtracking coverage. Without a firm understanding of backtracking, you won't be able to anticipate and identify backtracking-related problems. To help you catch runaway backtracking early, always test your regexes with long strings that contain partial matches. Think about the kinds of strings that your regexes will nearly but not quite match, and include those in your tests.
ÕâÒ²ÊDZ¾Õ³¤Æª´óÂÛ»ØËݵÄÔÒòÖ®Ò»¡£Èç¹ûûÓÐÈ·ÇÐÀí½â»ØËÝ£¬¾ÍÎÞ·¨Ô¤²âºÍÈ·¶¨»ØËÝÏà¹ØÎÊÌ⡣Ϊ°ïÖúÄãÔçÈÕ°ÑÎÕ»ØËÝʧ¿Ø£¬×ÜÊÇÓðüº¬ÌØÊâÆ¥ÅäµÄ³¤×Ö·û´®²âÊÔÄãµÄÕýÔò±í´ïʽ¡£Õë¶ÔÄãµÄÕýÔò±í´ïʽ¹¹Ë¼Ò»Ð©½üËƵ«²»ÄÜÍêÈ«Æ¥ÅäµÄ×Ö·û´®£¬½«ËûÃÇÓ¦ÓÃÔÚÄãµÄ²âÊÔÖС£
More Ways to Improve Regular Expression Efficiency Ìá¸ßÕýÔò±í´ïʽЧÂʵĸü¶à·½·¨
The following are a variety of additional regex efficiency techniques. Several of the points here have already been touched upon during the backtracking discussion.
ÏÂÃæÊÇһдÌá¸ßÕýÔò±í´ïʽЧÂʵļ¼Êõ¡£¼¸¸ö¼¼ÊõµãÒѾÔÚ»ØËݲ¿·ÖÌÖÂÛ¹ýÁË¡£
Focus on failing faster
¹Ø×¢ÈçºÎÈÃÆ¥Åä¸ü¿ìʧ°Ü
Slow regex processing is usually caused by slow failure rather than slow matching. This is compounded by the fact that if you're using a regex to match small parts of a large string, the regex will fail at many more positions than it will succeed. A change that makes a r
Ïà¹ØÎĵµ£º
ÃæÏò¶ÔÏóµÄÓïÑÔ¶àÊý¶¼Ö§³Ö¼Ì³Ð£¬¼Ì³Ð×îÖØÒªµÄÓŵã¾ÍÊÇ´úÂ븴Ó㬴Ӷø¹¹½¨´óÐÍÈí¼þϵͳ¡£Èç¹ûÒ»¸öÀàÄܹ»ÖØÓÃÁíÒ»¸öÀàµÄÊôÐԺͻò·½·¨£¬¾Í³Æ֮Ϊ¼Ì³Ð¡£
´ÓÕâ¸ö½Ç¶ÈÀ´¿´¿´jsµÄ¼Ì³Ð·½Ê½¡£jsÖм̳з½Ê½ÓëдÀ෽ʽϢϢÏà¹Ø¡£²»Í¬µÄдÀ෽ʽÔì³É²»Í¬µÄ¼Ì³Ð·½Ê½¡£¸÷ÖÖÁ÷ÐÐjs¿â¼Ì³Ð·½Ê½Ò²¸÷²»Ïàͬ¡£´Ó×î¼òµ¥µÄ
¸´ÓÿªÊ¼¡£
1¡¢¹¹Ôìº ......
2¡¢ÔÐÍ·½Ê½Ð´À࣬ÔÐÍ·½Ê½¼Ì³Ð
core js×ÔÉíµÄ¶ÔÏóϵͳ¾ÍÊDzÉÓÃÔÐÍ·½Ê½(prototype based)¼Ì³ÐµÄ¡£»òÕß˵core
jsûÓвÉÓó£¼ûµÄÀà¼Ì³Ð(class
based)ϵͳ£¬¶øÊÇʹÓÃÔÐͼ̳ÐÀ´ÊµÏÖ×Ô¼ºµÄ¶ÔÏóϵͳ¡£¹¤×÷ÖÐÎÒÃÇÒ²¿ÉÒÔÓÃÔÐÍ·½Ê½À´ÊµÏּ̳У¬´úÂ븴ÓÃÒÔ¹¹½¨×Ô¼ºµÄ¹¦ÄÜÄ£¿é¡£
/**
* ¸¸ÀàPolygon:¶à±ßÐÎ
*
*/
functio ......
µÚËÄÕ 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 ......
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 ......
µÚÎåÕ Strings and Regular Expressions ×Ö·û´®ºÍÕýÔò±í´ïʽ
Practically all JavaScript programs are intimately tied to strings. For example, many applications use Ajax to fetch strings from a server, convert those strings into more easily usable JavaScript objects, and ......