Ò׽ؽØÍ¼Èí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

[·­Òë]High Performance JavaScript(018)

String Trimming  ×Ö·û´®ÐÞ¼ô
    Removing leading and trailing whitespace from a string is a simple but common task. Although ECMAScript 5 adds a native string trim method (and you should therefore start to see this method in upcoming browsers), JavaScript has not historically included it. For the current browser crop, it's still necessary to implement a trim method yourself or rely on a library that includes it.
    È¥³ý×Ö·û´®Ê×βµÄ¿Õ¸ñÊÇÒ»¸ö¼òµ¥¶ø³£¼ûµÄÈÎÎñ¡£ËäÈ»ECMAScript 5Ìí¼ÓÁËÔ­Éú×Ö·û´®ÐÞ¼ôº¯Êý£¨ÄãÓ¦¸Ã¿ÉÒÔÔÚ¼´½«³öÏÖµÄä¯ÀÀÆ÷Öп´µ½ËüÃÇ£©£¬µ½Ä¿Ç°ÎªÖ¹JavaScript»¹Ã»Óаüº¬Ëü¡£¶Ôµ±Ç°µÄä¯ÀÀÆ÷¶øÑÔ£¬ÓбØÒª×Ô¼ºÊµÏÖÒ»¸öÐÞ¼ôº¯Êý£¬»òÕßÒÀ¿¿Ò»¸ö°üº¬´Ë¹¦ÄܵĿ⡣
    Trimming strings is not a common performance bottleneck, but it serves as a decent case study for regex optimization since there are a variety of ways to implement it.
    ÐÞ¼ô×Ö·û´®²»ÊÇÒ»¸ö³£¼ûµÄÐÔÄÜÆ¿¾±£¬µ«×÷ΪѧϰÕýÔò±í´ïʽÓÅ»¯µÄÀý×ÓÓжàÖÖʵÏÖ·½·¨¡£
Trimming with Regular Expressions  ÓÃÕýÔò±í´ïʽÐÞ¼ô
    Regular expressions allow you to implement a trim method with very little code, which is important for JavaScript libraries that focus on file size. Probably the best all-around solution is to use two substitutions—one to remove leading whitespace and another to remove trailing whitespace. This keeps things simple and fast, especially with long strings.
    ÕýÔò±í´ïʽÔÊÐíÄãÓúÜÉٵĴúÂëʵÏÖÒ»¸öÐÞ¼ôº¯Êý£¬Õâ¶ÔJavaScript¹ØÐÄÎļþ´óСµÄ¿âÀ´ËµÊ®·ÖÖØÒª¡£¿ÉÄÜ×îºÃµÄÈ«Ãæ½â¾ö·½°¸ÊÇʹÓÃÁ½¸ö×Ó±í´ïʽ£ºÒ»¸öÓÃÓÚÈ¥³ýÍ·²¿¿Õ¸ñ£¬ÁíÒ»¸öÓÃÓÚÈ¥³ýβ²¿¿Õ¸ñ¡£ÕâÑù´¦Àí¼òµ¥¶øÑ¸ËÙ£¬ÌرðÊÇ´¦Àí³¤×Ö·û´®Ê±¡£
if (!String.prototype.trim) {
  String.prototype.trim = function() {
    return this.replace(/^\s+/, "").replace(/\s+$/, "");
  }
}
// test the new method...
// tab (\t) and line feed (\n) characters are
// included in the leading whitespace.
var str = " \t\n test string ".trim();
alert(str == "tes


Ïà¹ØÎĵµ£º

javascriptЧÂʾ­Ñé̸(Ò»)

±¾ÎÄÖ÷ÒªÊdzöÓÚÓÐÅóÓÑʹÓÃÎÒÔ­À´Ð´µÄautocompleteµÄJS¿Ø¼þ¡£µ±Êý¾ÝÁ¿´óµÄʱºò£¬»á³öÏÖЧÂʼ«ÆäÂýµÄÇé¿ö£¬ÎÒÔÚÕâ¶Îʱ¼ä×ö³öµÄһЩ²âÊÔÒ²¼°Ò»Ð©¾­Ñ飬Óë´ó¼Ò·ÖÏí£¬Èç¹ûÓдíµÄµØ·½£¬»¹ÇëÖ¸³ö¡£
¾­¹ý²âÊÔ£¬ÎÒÃǻᷢÏÖÈçϵÄÇé¿ö»òÕß˵µÄ½áÂÛ£¬Èç¹ûÄúµÄ²âÊÔ½á¹ûÓëÎҵIJ»·û£¬Çë˵Ã÷Ô­Òò£¬ÒÔ±ãÏ໥ѧϰ¡£
1£©µ±Ò»¸ö½Ï´óµÄHTML×Ö· ......

javascript¿çä¯ÀÀÆ÷´´½¨XML¶ÔÏó


var
 
xmlDoc
 
=
 
null
;
function
 
parseXML
(
xmlUrl
)
{
¡¡¡¡try
 
{
¡¡¡¡¡¡¡¡//IE
¡¡¡¡¡¡¡¡xmlDoc
 
=
 
new
 
ActiveXObject
(
"Microsoft.XMLDOM"
);
¡¡¡¡¡¡¡¡xmlDoc
.
async
 
=
 
false
;
¡¡¡¡¡¡¡¡xmlDoc ......

JavaScript»ù´¡ÖªÊ¶2

ÍæPHP¡¢Delphi¡¢Java»ù±¾É϶¼ÓжÔÏó£¬Ï°¹ßÕâÖÖ˼·ºóÉÏÊÖÈκÎÓïÑÔ¶¼Ïë¿¿OO˼·£¬Õâ¾ø²»ÊÇÔÚ¸Ïʱ÷Ö£¬¶øÊǰÑÏà¹Ø´úÂë½øÐÐÄÚ¾ÛµÄÈ·¿ÉÒÔÌå»áµ½Î¬»¤µÄ·½±ã£¡
ÔÚJavaScriptÖÐÈçºÎ´´½¨¶ÔÏó£¿
JavaScriptÊÇ»ùÓÚ¶ÔÏóµÄ£¡ËüÒ²ÊÇÒÔObjectΪ¸ùÀ࣬ÆäËüÀà¼Ì³ÐÖ®¡£ÔÚ¸ùÀàÌṩÁ˼¸¸ö·½·¨¡£¹©¼Ì³ÐÀàʹÓã¡
ÒÔÏÂÊÇ´´½¨¶ÔÏóµÄÀý×Ó£º
funct ......

[·­Òë]High Performance JavaScript(014)

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 ......

[·­Òë]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 o ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØÍ¼ | ¸ÓICP±¸09004571ºÅ