[·Òë]High Performance JavaScript(029)
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 static resources whenever they change.
³ä·ÖÀûÓûº´æ¿ØÖÆ¿ÉÕæÕýÌá¸ßÓû§ÌåÑ飬µ«ËüÓÐÒ»¸öȱµã£ºµ±Ó¦ÓóÌÐò¸üÐÂÖ®ºó£¬ÄãÏ£ÍûÈ·±£Óû§µÃµ½¾²Ì¬ÄÚÈݵÄ×îа汾¡£Õâͨ¹ý¶Ô¸Ä¶¯µÄ¾²Ì¬×ÊÔ´½øÐÐÖØÃüÃûʵÏÖ¡£
Most often, developers add a version or a build number to filenames. Others like to append a checksum. Personally, I like to use a timestamp. This task can be automated using Ant. The following target takes care of renaming JavaScript files by appending a timestamp in the form of yyyyMMddhhmm:
´ó¶àÇé¿öÏ£¬¿ª·¢ÕßÏòÎļþÃûÖÐÌí¼ÓÒ»¸ö°æ±¾ºÅ»ò¿ª·¢±àºÅ¡£ÓÐÈËϲ»¶×·¼ÓÒ»¸öУÑéºÍ¡£¸öÈ˶øÑÔ£¬ÎÒ¸üϲ»¶Ê±¼ä´Á¡£´ËÈÎÎñ¿ÉÓÃAnt×Ô¶¯Íê³É¡£ÏÂÃæµÄÄ¿±êÌåͨ¹ý¸½¼ÓÒ»¸öyyyyMMddhhmm¸ñʽµÄʱ¼ä´ÁÖØÃûÃûJavaScriptÎļþ£º
<target name="js.copy">
<!-- Create the time stamp -->
<tstamp/>
<!-- Rename JavaScript files by appending a time stamp -->
<copy todir="${build.dir}">
<fileset dir="${src.dir}" includes="*.js"/>
<globmapper from="*.js" to="*-${DSTAMP}${TSTAMP}.js"/>
</copy>
</target>
Using a Content Delivery Network ʹÓÃÄÚÈÝ´«µÝÍø
A content delivery network (CDN) is a network of computers distributed geographically across the Internet that is responsible for delivering content to end users. The primary reasons for using a CDN are reliability, scalability, and above all, performance. In fact, by serving content from the location closest to the user, CDNs are able to dramatically decrease network latency.
ÄÚÈÝ´«µÝÍøÂ磨CDN£©Êǰ´ÕÕµØÀí·Ö²¼µÄ¼ÆËã»úÍøÂ磬ͨ¹ýÒÔÌ«Íø¸ºÔðÏò×îÖÕÓû§·Ö
Ïà¹ØÎĵµ£º
Yielding with Timers Óö¨Ê±Æ÷Èóöʱ¼äƬ
Despite your best efforts, there will be times when a JavaScript task cannot be completed in 100 milliseconds or less because of its complexity. In these cases, it's ideal to yield control of the UI thread so that UI updates may occur ......
Splitting Up Tasks ·Ö½âÈÎÎñ
What we typically think of as one task can often be broken down into a series of subtasks. If a single function is taking too long to execute, check to see whether it can be broken down into a series of smaller functions that complete in smaller ......
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:
×ܵÄÀ´ËµÔ½ÇáÁ¿¼¶µÄ¸ñʽԽºÃ£¬× ......
µÚ°ËÕ Programming Practices ±à³Ìʵ¼ù
Every programming language has pain points and inefficient patterns that develop over time. The appearance of these traits occurs as people migrate to the language and start pushing its boundaries. Since 2005, when the term "Ajax" ......
Use the Fast Parts ʹÓÃËÙ¶È¿ìµÄ²¿·Ö
Even though JavaScript is often blamed for being slow, there are parts of the language that are incredibly fast. This should come as no surprise, since JavaScript engines are built in lower-level languages and are therefore compiled. Thou ......