[·Òë]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£©Êǰ´ÕÕµØÀí·Ö²¼µÄ¼ÆËã»úÍøÂ磬ͨ¹ýÒÔÌ«Íø¸ºÔðÏò×îÖÕÓû§·Ö
Ïà¹ØÎĵµ£º
ʹÓÃ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( ......
function total(){
var i=0;
for(j=1;j<=20;j++)
{
var step="step"+j;
if(document.getElementById(step)){
if(document.getElementById(step).checked==true)
{
i=i+parseInt(document.getElementById(step).value);
}
}
}
document.getElementById("total").innerHTML = i;
}
function Resetvalue(){
......
Conditionals Ìõ¼þ±í´ïʽ
Similar in nature to loops, conditionals determine how execution flows through JavaScript. The traditional argument of whether to use if-else statements or a switch statement applies to JavaScript just as it does to other languages. Since different b ......
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:
×ܵÄÀ´ËµÔ½ÇáÁ¿¼¶µÄ¸ñʽԽºÃ£¬× ......