[·Òë]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¡¢¹¹Ôìº ......
£¼script language="javaScript"£¾
function closeWindow()
{
¡¡window.opener = null;
¡¡window.open(' ', '_self', ' ');
¡¡window.close();
}
£¼/script£¾
£¼input type='button' value='¹Ø±Õ´°¿Ú' onClick="closeWindow()"£¾
»ò
£¼input type="button" value="¹ ......
ʹÓÃjavascript´´½¨Microsoft XML DOM,¾Í¿ÉÒÔÍê³ÉÕâÒ»¹¤×÷.
// ×°ÈëÊý¾Ý.
var source = new ActiveXObject("Microsoft.XMLDOM");
source.async = false
source.load("history.xml");
// ×°ÈëÑùʽ±í.
var stylesheet = new ActiveXObject("Microsoft.XMLDOM");
stylesheet.async = false
stylesheet.load( ......
AJAX (Òì²½ JavaScript ºÍ XML) ÊǸöвúÉúµÄÊõÓï,רΪÃèÊöJavaScriptµÄÁ½ÏîÇ¿´óÐÔÄÜ.ÕâÁ½ÏîÐÔ
ÄÜÔÚ¶àÄêÀ´Ò»Ö±±»ÍøÂ翪·¢ÕßËùºöÂÔ,Ö±µ½×î½üGmail, Google suggestºÍgoogle MapsµÄºá¿Õ³öÊÀ²ÅʹÈË
ÃÇ¿ªÊ¼Òâʶµ½ÆäÖØÒªÐÔ.
ÕâÁ½Ïî±»ºöÊÓµÄÐÔÄÜÊÇ:
* ÎÞÐèÖØÐÂ×°ÔØÕû¸öÒ³Ãæ±ãÄÜÏò·þÎñÆ÷·¢ËÍÇëÇó.
* ¶ÔXMLÎĵµµÄ½âÎöºÍ´¦Àí£®
......
µÚËÄÕ 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 ......