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

[·­Òë]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/.
    Çë×¢Ò⣬ÕâЩÊ


Ïà¹ØÎĵµ£º

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

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

[·­Òë]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(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 in ......

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

Data Formats  Êý¾Ý¸ñʽ
    When considering data transmission techniques, you must take into account several factors: feature set, compatibility, performance, and direction (to or from the server). When considering data formats, the only scale you need for comparison is speed.
......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØÍ¼ | ¸ÓICP±¸09004571ºÅ