[·Òë]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.
ÕâЩ¹¤¾ßÔÚÄÇЩÏÖ´ú´úÂë
Ïà¹ØÎĵµ£º
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 ......
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 ......
µÚÁùÕ 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 ......
µÚÆßÕ Ajax Òì²½JavaScriptºÍXML
Ajax is a cornerstone of high-performance JavaScript. It can be used to make a page load faster by delaying the download of large resources. It can prevent page loads altogether by allowing for data to be transferred between the client ......
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:
×ܵÄÀ´ËµÔ½ÇáÁ¿¼¶µÄ¸ñʽԽºÃ£¬× ......