[·Òë]High Performance JavaScript(030)
µÚʮՠTools ¹¤¾ß
Having the right software is essential for identifying bottlenecks in both the loading and running of scripts. A number of browser vendors and large-scale websites have shared techniques and tools to help make the Web faster and more efficient. This chapter focuses on some of the free tools available for:
µ±È·¶¨½Å±¾¼ÓÔغÍÔËÐÐʱµÄÆ¿¾±ËùÔÚʱ£¬ºÏÊֵŤ¾ßÊDZز»¿ÉÉٵġ£Ðí¶àä¯ÀÀÆ÷³§É̺ʹóÐÍÍøÕ¾·ÖÏíÁËһЩ¼¼ÊõºÍ¹¤¾ß£¬°ïÖú¿ª·¢ÕßʹÍøÒ³¸ü¿ì£¬Ð§Âʸü¸ß¡£±¾Õ¹Ø×¢ÓÚÕâЩÃâ·Ñ¹¤¾ß£º
Profiling ÐÔÄÜ·ÖÎö
Timing various functions and operations during script execution to identify areas for optimization
Ôڽű¾ÔËÐÐÆÚ¶¨Ê±Ö´Ðв»Í¬º¯ÊýºÍ²Ù×÷£¬ÕÒ³öÐèÒªÓÅ»¯µÄ²¿·Ö
Network analysis ÍøÂç·ÖÎö
Examining the loading of images, stylesheets, and scripts and their effect on overall page load and rendering
¼ì²éͼƬ£¬Ñùʽ±í£¬ºÍ½Å±¾µÄ¼ÓÔعý³Ì£¬»ã±¨ËüÃǶÔÕû¸öÒ³Ãæ¼ÓÔغÍäÖȾµÄÓ°Ïì
When a particular script or application is performing less than optimally, a profiler can help prioritize areas for optimization. This can get tricky because of the range of supported browsers, but many vendors now provide a profiler along with their debugging tools. In some cases, performance issues may be specific to a particular browser; other times, the symptoms may occur across multiple browsers. Keep in mind that the optimizations applied to one browser might benefit other browsers, but they might have the opposite effect as well. Rather than assuming which functions or operations are slow, profilers ensure that optimization time is spent on the slowest areas of the system that affect the most browsers.
µ±Ò»¸öÌض¨µÄ½Å±¾»òÓ¦ÓóÌÐòûÓдﵽ×îÓÅ״̬ʱ£¬Ò»¸öÐÔÄÜ·ÖÎöÆ÷ÓÐÖúÓÚ°²ÅÅÓÅ»¯¹¤×÷µÄÏȺó´ÎÐò¡£²»¹ý£¬ÒòΪä¯ÀÀÆ÷Ö§³ÖµÄ·¶Î§²»Í¬£¬Õâ¿ÉÄܱäµÃºÜÂé·³£¬µ«Ðí¶à³§ÉÌÔÚËûÃǵĵ÷ÊÔ¹¤¾ßÖÐÌṩÁËÐÔÄÜ·ÖÎöÆ÷¡£ÓÐЩÇé¿öÏ£¬ÐÔÄÜÎÊÌâ¿ÉÄÜÓëÌض¨ä¯ÀÀÆ÷
Ïà¹ØÎĵµ£º
±¾ÎÄÖ÷ÒªÊdzöÓÚÓÐÅóÓÑʹÓÃÎÒÔÀ´Ð´µÄautocompleteµÄJS¿Ø¼þ¡£µ±Êý¾ÝÁ¿´óµÄʱºò£¬»á³öÏÖЧÂʼ«ÆäÂýµÄÇé¿ö£¬ÎÒÔÚÕâ¶Îʱ¼ä×ö³öµÄһЩ²âÊÔÒ²¼°Ò»Ð©¾Ñ飬Óë´ó¼Ò·ÖÏí£¬Èç¹ûÓдíµÄµØ·½£¬»¹ÇëÖ¸³ö¡£
¾¹ý²âÊÔ£¬ÎÒÃǻᷢÏÖÈçϵÄÇé¿ö»òÕß˵µÄ½áÂÛ£¬Èç¹ûÄúµÄ²âÊÔ½á¹ûÓëÎҵIJ»·û£¬Çë˵Ã÷ÔÒò£¬ÒÔ±ãÏ໥ѧϰ¡£
1£©µ±Ò»¸ö½Ï´óµÄHTML×Ö· ......
ʹÓÃjavascript´´½¨Microsoft XML DOM,¾Í¿ÉÒÔÍê³ÉÕâÒ»¹¤×÷.
// ×°ÈëÊý¾Ý.
var source = new ActiveXObject("Microsoft.XMLDOM");
source.async = false
source.load("history.xml");
// ×°ÈëÑùʽ±í.
var stylesheet = new ActiveXObject("Microsoft.XMLDOM");
stylesheet.async = false
stylesheet.load( ......
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 ......
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:
×ܵÄÀ´ËµÔ½ÇáÁ¿¼¶µÄ¸ñʽԽºÃ£¬× ......
µÚ¾ÅÕÂ
Building and Deploying High-Performance JavaScript Applications
´´½¨²¢²¿Êð¸ßÐÔÄÜJavaScriptÓ¦ÓóÌÐò
According to a 2007 study by Yahoo!'s Exceptional Performance team, 40%–60% of Yahoo!'s users have an empty cache experience, and about 20% of all page views are done ......