[·Òë]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
Ïà¹ØÎĵµ£º
3¡¢×éºÏ¹¹Ô캯Êý/ÔÐÍ·½Ê½Ð´À࣬²ÉÓÃÇ°ÃæÖÖ·½Ê½¼Ì³Ð
ÕâÖÖ·½Ê½¸¸À࣬×ÓÀàµÄÊôÐÔ¶¼¹ÒÔÚ¹¹Ô캯ÊýÀ·½·¨¶¼¹ÒÔÚÔÐÍÉÏ¡£
/**
* ¸¸ÀàPolygon:¶à±ßÐÎ
*/
function Polygon(sides) {
this.sides = sides;
}
Polygon.prototype.setSides = function(s) {this.sides=s;}
/**
* Triangle Èý½ÇÐÎ
* @param {Object} b ......
[ת×Ô]http://article.yeeyan.org/view/mouse4x/16540
JSON±»¹«ÈÏΪä¯ÀÀÆ÷ÖÐXMLµÄºó¼ÌÕߣ¬ËüµÄÄ¿±ê½ö½öÊdzÉΪһÖÖ¼òµ¥¡¢ÓÅÑŵÄÊý¾Ý¸ñʽ£¬ÒÔ·½±ãä¯ÀÀÆ÷ºÍ·þÎñÆ÷Ö®¼äµÄÊý¾Ý½»»»¡£ÔÚÍê³ÉÕâÒ»¼òµ¥ÈÎÎñµÄ¹ý³ÌÖУ¬Ëü½«ÒýÁìÏÂÒ»´úÍòÎ¬Íø¡£
¶ÔÏó¼ò½é
¿´,Õâ¾ÍÊÇÒ»¸ö¶ÔÏó:
var myFirstObject ={};
¾¡¹Ü¿´ÆðÀ´Í¦¼òµ¥£¬È»¶øÄÇЩ» ......
³£¹æµÄ·½·¨Êǽ«ÄêÔÂÈÕÈ¡³ö£¬È»ºó·Ö±ðÅжϷ¶Î§£¬È»ºó¾ÍÅжÏÈòÄê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;
......
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 ......
µÚÎåÕ 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 ......