[·Òë]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£©Êǰ´ÕÕµØÀí·Ö²¼µÄ¼ÆËã»úÍøÂ磬ͨ¹ýÒÔÌ«Íø¸ºÔðÏò×îÖÕÓû§·Ö
Ïà¹ØÎĵµ£º
Òþ²Ø³ÉÔ±±äÁ¿
ÔÚº¯ÊýÌåÄÚ¶¨ÒåµÄ±äÁ¿Îª¾Ö²¿±äÁ¿£¬À뿪º¯Êý¾Í¹ÒµôÁË
ÔÚº¯ÊýÌåÄÚʹÓÃthis.³ÉÔ±±äÁ¿Ãû£¬ÔòΪwindow¶ÔÏó¼¶±äÁ¿£¬¼´È«¾Ö±äÁ¿
¹ÊÐèÒªÕâÑùÒþ²Ø³ÉÔ±±äÁ¿£¬ÏòÍâÖ»±©Â¶get¡¢setº¯Êý
function testClass(name){
var _firstname=name;
return {
getname : function() {
return _fir ......
AJAX (Òì²½ JavaScript ºÍ XML) ÊǸöвúÉúµÄÊõÓï,רΪÃèÊöJavaScriptµÄÁ½ÏîÇ¿´óÐÔÄÜ.ÕâÁ½ÏîÐÔ
ÄÜÔÚ¶àÄêÀ´Ò»Ö±±»ÍøÂ翪·¢ÕßËùºöÂÔ,Ö±µ½×î½üGmail, Google suggestºÍgoogle MapsµÄºá¿Õ³öÊÀ²ÅʹÈË
ÃÇ¿ªÊ¼Òâʶµ½ÆäÖØÒªÐÔ.
ÕâÁ½Ïî±»ºöÊÓµÄÐÔÄÜÊÇ:
* ÎÞÐèÖØÐÂ×°ÔØÕû¸öÒ³Ãæ±ãÄÜÏò·þÎñÆ÷·¢ËÍÇëÇó.
* ¶ÔXMLÎĵµµÄ½âÎöºÍ´¦Àí£®
......
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(){
......
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 ......