[·Òë]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/.
Çë×¢Ò⣬ÕâЩÊ
Ïà¹ØÎĵµ£º
±¾ÎÄÖ÷ÒªÊdzöÓÚÓÐÅóÓÑʹÓÃÎÒÔÀ´Ð´µÄautocompleteµÄJS¿Ø¼þ¡£µ±Êý¾ÝÁ¿´óµÄʱºò£¬»á³öÏÖЧÂʼ«ÆäÂýµÄÇé¿ö£¬ÎÒÔÚÕâ¶Îʱ¼ä×ö³öµÄһЩ²âÊÔÒ²¼°Ò»Ð©¾Ñ飬Óë´ó¼Ò·ÖÏí£¬Èç¹ûÓдíµÄµØ·½£¬»¹ÇëÖ¸³ö¡£
¾¹ý²âÊÔ£¬ÎÒÃǻᷢÏÖÈçϵÄÇé¿ö»òÕß˵µÄ½áÂÛ£¬Èç¹ûÄúµÄ²âÊÔ½á¹ûÓëÎҵIJ»·û£¬Çë˵Ã÷ÔÒò£¬ÒÔ±ãÏ໥ѧϰ¡£
1£©µ±Ò»¸ö½Ï´óµÄHTML×Ö· ......
Repaints and Reflows ÖØ»æºÍÖØÅÅ°æ
Once the browser has downloaded all the components of a page—HTML markup, JavaScript, CSS, images—it parses through the files and creates two internal data structures:
µ±ä¯ÀÀÆ÷ÏÂÔØÍêËùÓÐÒ³ÃæHTML±ê¼Ç£¬JavaScri ......
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 ......
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 ......
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.
......