[·Òë]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.
µ±Ò»¸öÌØ¶¨µÄ½Å±¾»òÓ¦ÓóÌÐòûÓдﵽ×îÓÅ״̬ʱ£¬Ò»¸öÐÔÄÜ·ÖÎöÆ÷ÓÐÖúÓÚ°²ÅÅÓÅ»¯¹¤×÷µÄÏȺó´ÎÐò¡£²»¹ý£¬ÒòΪä¯ÀÀÆ÷Ö§³ÖµÄ·¶Î§²»Í¬£¬Õâ¿ÉÄܱäµÃºÜÂé·³£¬µ«Ðí¶à³§ÉÌÔÚËûÃǵĵ÷ÊÔ¹¤¾ßÖÐÌṩÁËÐÔÄÜ·ÖÎöÆ÷¡£ÓÐЩÇé¿öÏ£¬ÐÔÄÜÎÊÌâ¿ÉÄÜÓëÌØ¶¨ä¯ÀÀÆ÷
Ïà¹ØÎĵµ£º
ʹÓÃ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( ......
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 ......
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:
×ܵÄÀ´ËµÔ½ÇáÁ¿¼¶µÄ¸ñʽԽºÃ£¬× ......
JavaScript Minification JavaScript½ô´Õ
JavaScript minification is the process by which a JavaScript file is stripped of everything that does not contribute to its execution. This includes comments and unnecessary whitespace. The process typically reduces the file size by ha ......
Working Around Caching Issues ¹ØÓÚ»º´æÎÊÌâ
Adequate cache control can really enhance the user experience, but it has a downside: when revving up your application, you want to make sure your users get the latest version of the static content. This is accomplished by renaming ......