[·Òë]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
Ïà¹ØÎĵµ£º
2¡¢ÔÐÍ·½Ê½Ð´À࣬ÔÐÍ·½Ê½¼Ì³Ð
core js×ÔÉíµÄ¶ÔÏóϵͳ¾ÍÊDzÉÓÃÔÐÍ·½Ê½(prototype based)¼Ì³ÐµÄ¡£»òÕß˵core
jsûÓвÉÓó£¼ûµÄÀà¼Ì³Ð(class
based)ϵͳ£¬¶øÊÇʹÓÃÔÐͼ̳ÐÀ´ÊµÏÖ×Ô¼ºµÄ¶ÔÏóϵͳ¡£¹¤×÷ÖÐÎÒÃÇÒ²¿ÉÒÔÓÃÔÐÍ·½Ê½À´ÊµÏּ̳У¬´úÂ븴ÓÃÒÔ¹¹½¨×Ô¼ºµÄ¹¦ÄÜÄ£¿é¡£
/**
* ¸¸ÀàPolygon:¶à±ßÐÎ
*
*/
functio ......
JavaScriptʼþ´óÈ«
click() ¶ÔÏó.click() ʹ¶ÔÏó±»µã»÷¡£
closed ¶ÔÏó.closed ¶ÔÏó´°¿ÚÊÇ·ñÒѹرÕtrue/false
clearTimeout(¶ÔÏó) Çå³ýÒÑÉèÖõÄsetTimeout¶ÔÏó
clearInterval(¶ÔÏó) Çå³ýÒÑÉèÖõÄsetInterval¶ÔÏó
confirm("ÌáʾÐÅÏ¢") µ¯³öÈ·ÈÏ¿ò£¬È·¶¨·µ»ØtrueÈ¡Ïû·µ»Øfalse
cursor:Ñùʽ ¸ü¸ÄÊó±êÑùÊ ......
³£¹æµÄ·½·¨Êǽ«ÄêÔÂÈÕÈ¡³ö£¬È»ºó·Ö±ðÅжϷ¶Î§£¬È»ºó¾ÍÅжÏÈòÄê2ÔµÄÌìÊý
¿ÉÒÔͨ¹ýnew Date(string)µÄ¹¹Ô죬±È½ÏÄêÔÂÈÕ×Ö·ûÊÇ·ñ·¢Éú±ä»¯Åжϡ£
function CheckDate(text) {
if (!text) return false;
text = text.replace(/[\/-]0?/g, "/");
if (!text.match(/^\d{4}\/\d{1,2}\/\d{1,2}$/)) return true;
......
AJAX (Òì²½ JavaScript ºÍ XML) ÊǸöвúÉúµÄÊõÓï,רΪÃèÊöJavaScriptµÄÁ½ÏîÇ¿´óÐÔÄÜ.ÕâÁ½ÏîÐÔ
ÄÜÔÚ¶àÄêÀ´Ò»Ö±±»ÍøÂ翪·¢ÕßËùºöÂÔ,Ö±µ½×î½üGmail, Google suggestºÍgoogle MapsµÄºá¿Õ³öÊÀ²ÅʹÈË
ÃÇ¿ªÊ¼Òâʶµ½ÆäÖØÒªÐÔ.
ÕâÁ½Ïî±»ºöÊÓµÄÐÔÄÜÊÇ:
* ÎÞÐèÖØÐÂ×°ÔØÕû¸öÒ³Ãæ±ãÄÜÏò·þÎñÆ÷·¢ËÍÇëÇó.
* ¶ÔXMLÎĵµµÄ½âÎöºÍ´¦Àí£®
......
function total(){
var i=0;
for(j=1;j<=20;j++)
{
var step="step"+j;
if(document.getElementById(step)){
if(document.getElementById(step).checked==true)
{
i=i+parseInt(document.getElementById(step).value);
}
}
}
document.getElementById("total").innerHTML = i;
}
function Resetvalue(){
......