[·Òë]High Performance JavaScript(033)
Summary ×ܽá
When web pages or applications begin to feel slow, analyzing assets as they come over the wire and profiling scripts while they are running allows you to focus your optimization efforts where they are needed most.
µ±ÍøÒ³»òÓ¦ÓóÌÐò±äÂýʱ£¬·ÖÎöÍøÉÏ´«À´µÄ×ÊÔ´£¬·ÖÎö½Å±¾µÄÔËÐÐÐÔÄÜ£¬Ê¹ÄãÄܹ»¼¯Öо«Á¦ÔÚÄÇЩÐèҪŬÁ¦ÓÅ»¯µÄµØ·½¡£
• Use a network analyzer to identify bottlenecks in the loading of scripts and other page assets; this helps determine where script deferral or profiling may be needed.
ʹÓÃÍøÂç·ÖÎöÆ÷ÕÒ³ö¼ÓÔؽű¾ºÍÆäËüÒ³Ãæ×ÊÔ´µÄÆ¿¾±ËùÔÚ£¬ÕâÓÐÖúÓÚ¾ö¶¨ÄÄЩ½Å±¾ÐèÒªÑÓ³Ù¼ÓÔØ£¬»òÕß½øÐнøÒ»²½·ÖÎö¡£
• Although conventional wisdom says to minimize the number of HTTP requests, deferring scripts whenever possible allows the page to render more quickly, providing users with a better overall experience.
´«Í³µÄÖǻ۸æËßÎÒÃÇÓ¦¾¡Á¿¼õÉÙHTTPÇëÇóµÄÊýÁ¿£¬¾¡Á¿ÑÓ³Ù¼ÓÔؽű¾ÒÔʹҳÃæäÖȾËٶȸü¿ì£¬ÏòÓû§Ìṩ¸üºÃµÄÕûÌåÌåÑé¡£
• Use a profiler to identify slow areas in script execution, examining the time spent in each function, the number of times a function is called, and the callstack itself provides a number of clues as to where optimization efforts should be focused.
ʹÓÃÐÔÄÜ·ÖÎöÆ÷ÕÒ³ö½Å±¾ÔËÐÐʱËÙ¶ÈÂýµÄ²¿·Ö£¬¼ì²éÿ¸öº¯ÊýËù»¨·ÑµÄʱ¼ä£¬ÒÔ¼°º¯Êý±»µ÷ÓõĴÎÊý£¬Í¨¹ýµ÷ÓÃÕ»×ÔÉíÌṩµÄһЩÏßË÷À´ÕÒ³öÄÄЩµØ·½Ó¦µ±Å¬Á¦ÓÅ»¯¡£
• Although time spent and number of calls are usually the most valuable bits of data, looking more closely at how functions are being called might yield other optimization candidates.
ËäÈ»»¨·Ñʱ¼äºÍµ÷ÓôÎÊýͨ³£ÊÇÊý¾ÝÖÐ×îÓмÛÖµµÄµã£¬»¹ÊÇÓ¦µ±×Ðϸ²ì¿´º¯ÊýµÄµ÷Óùý³Ì£¬¿ÉÄÜ·¢ÏÖÆäËüÓÅ»¯·½·¨¡£
These tools help to demystify the generally hostile programming environments that modern code must run in. Using them prior to beginning optimization will ensure that development time is spent focusing on the right problems.
ÕâЩ¹¤¾ßÔÚÄÇЩÏÖ´ú´úÂë
Ïà¹ØÎĵµ£º
µÚÁùÕ Responsive Interfaces ÏìÓ¦½Ó¿Ú
There's nothing more frustrating than clicking something on a web page and having nothing happen. This problem goes back to the origin of transactional web applications and resulted in the now-ubiquitous "please click only once" m ......
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 ......
Firebug
Firefox is a popular browser with developers, partially due to the Firebug addon (available at http://www.getfirebug.com/), which was developed initially by Joe Hewitt and is now maintained by the Mozilla Foundation. This tool has increased the productivity of web develop ......