[·Òë]High Performance JavaScript(024)
Data Format Conclusions Êý¾Ý¸ñʽ×ܽá
Favor lightweight formats in general; the best are JSON and a character-delimited custom format. If the data set is large and parse time becomes an issue, use one of these two techniques:
×ܵÄÀ´ËµÔ½ÇáÁ¿¼¶µÄ¸ñʽԽºÃ£¬×îºÃÊÇJSONºÍ×Ö·û·Ö¸ôµÄ×Ô¶¨Òå¸ñʽ¡£Èç¹ûÊý¾Ý¼¯ºÜ´ó»òÕß½âÎöʱ¼ä³ÉÎÊÌ⣬ÄÇô¾ÍʹÓÃÕâÁ½ÖÖ¸ñʽ֮һ£º
• JSON-P data, fetched using dynamic script tag insertion. This treats the data as executable JavaScript, not a string, and allows for extremely fast parsing. This can be used across domains, but shouldn't be used with sensitive data.
JSON-PÊý¾Ý£¬Óö¯Ì¬½Å±¾±êÇ©²åÈë·¨»ñÈ¡¡£Ëü½«Êý¾ÝÊÓΪ¿ÉÔËÐеÄJavaScript¶ø²»ÊÇ×Ö·û´®£¬½âÎöËٶȼ«¿ì¡£ËüÄܹ»¿çÓòʹÓ㬵«²»Ó¦Éæ¼°Ãô¸ÐÊý¾Ý¡£
• A character-delimited custom format, fetched using either XHR or dynamic script tag insertion and parsed using split(). This technique parses extremely large datasets slightly faster than the JSON-P technique, and generally has a smaller file size.
×Ö·û·Ö¸ôµÄ×Ô¶¨Òå¸ñʽ£¬Ê¹ÓÃXHR»ò¶¯Ì¬½Å±¾±êÇ©²åÈë¼¼ÊõÌáÈ¡£¬Ê¹ÓÃsplit()½âÎö¡£´Ë¼¼ÊõÔÚ½âÎö·Ç³£´óÊý¾Ý¼¯Ê±±ÈJSON-P¼¼ÊõÂԿ죬¶øÇÒͨ³£Îļþ³ß´ç¸üС¡£
The following table and Figure 7-1 show all of the performance numbers again (in order from slowest to fastest), so that you can compare each of the formats in one place. HTML is excluded, since it isn't directly comparable to the other formats.
ϱíºÍͼ7-1ÔÙ´ÎÏÔʾÁËËùÓз½·¨µÄÐÔÄÜÊý¾Ý£¨°´ÕÕ´ÓÂýµ½¿ìµÄ˳Ðò£©£¬Äã¿ÉÒÔÔڴ˱ȽÏÿÖÖ¸ñʽµÄÓÅÁÓ¡£HTMLδ°üÀ¨£¬ÒòΪËüÓëÆäËû¸ñʽ²»ÄÜÖ±½Ó±È½Ï¡£
Figure 7-1. A comparison of data format download and parse times
ͼ7-1 ¸÷ÖÖÊý¾Ý¸ñʽÏÂÔغͽâÎöµÄʱ¼ä
Keep in mind that these numbers are from a single test run in a single browser. The results should be used as general indicators of performance, not as hard numbers. You can run these tests yourself at http://techfoolery.com/formats/.
Çë×¢Ò⣬ÕâЩÊ
Ïà¹ØÎĵµ£º
Ò³ÃæÌá½»Êý¾ÝÒ»°ãÓÐÁ½ÖÖ·½·¨£ºget,post¡£post¾ÍÊÇËùνµÄformÌá½»£¬Ê¹ÓÃÊÓͼ£»getÊÇͨ¹ýurlÌá½»¡£
Get·½·¨Ò»°ãÓúǫ́´úÂ루Èçasp,asp.net£©»ñµÃ²ÎÊý£¬´úÂëºÜ¼òµ¥£ºRequest.QueryString["id"];¼´¿É»ñÈ¡¡£
ÓÐЩʱºòÐèÒªÖ±½ÓÔÚǰ̨»ñÈ¡url²ÎÊý£¬ÒªÓõ½javascript£¬jsûÓÐÖ±½Ó»ñÈ¡url²ÎÊýµÄ·½·¨£¬ÄÇô£¬ÎÒÃÇÈçºÎͨ¹ýjs ......
typeof ÊÇJavaScriptµÄÔËËã·û
——·µ»ØÒ»¸öÓÃÀ´±íʾ±í´ïʽµÄÊý¾ÝÀàÐ͵Ä×Ö·û´®
typeof[(] expression [)] //typeofÓï·¨ÖеÄÔ²À¨ºÅÊÇ¿ÉÑ¡Ïî
typeof ÔËËã·û@import url(../html-vss/msdnie4a.css);
typeof
ÔËËã·û°ÑÀàÐÍÐÅÏ¢µ±×÷×Ö·û´®·µ»Ø¡£typeof
·µ»ØÖµÓÐÁ ......
Òþ²Ø³ÉÔ±±äÁ¿
ÔÚº¯ÊýÌåÄÚ¶¨ÒåµÄ±äÁ¿Îª¾Ö²¿±äÁ¿£¬À뿪º¯Êý¾Í¹ÒµôÁË
ÔÚº¯ÊýÌåÄÚʹÓÃthis.³ÉÔ±±äÁ¿Ãû£¬ÔòΪwindow¶ÔÏ󼶱äÁ¿£¬¼´È«¾Ö±äÁ¿
¹ÊÐèÒªÕâÑùÒþ²Ø³ÉÔ±±äÁ¿£¬ÏòÍâÖ»±©Â¶get¡¢setº¯Êý
function testClass(name){
var _firstname=name;
return {
getname : function() {
return _fir ......
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 re ......
Splitting Up Tasks ·Ö½âÈÎÎñ
What we typically think of as one task can often be broken down into a series of subtasks. If a single function is taking too long to execute, check to see whether it can be broken down into a series of smaller functions that complete in smaller ......