[·Òë]High Performance JavaScript(005)
µÚ¶þÕ Data Access Êý¾Ý·ÃÎÊ
One of the classic computer science problems is determining where data should be stored for optimal reading and writing. Where data is stored is related to how quickly it can be retrieved during code execution. This problem in JavaScript is somewhat simplified because of the small number of options for data storage. Similar to other languages, though, where data is stored can greatly affect how quickly it can be accessed later. There are four basic places from which data can be accessed in JavaScript:
¾µä¼ÆËã»ú¿ÆѧµÄÒ»¸öÎÊÌâÊÇÈ·¶¨Êý¾ÝÓ¦µ±´æ·ÅÔÚʲôµØ·½£¬ÒÔʵÏÖ×î¼ÑµÄ¶ÁдЧÂÊ¡£Êý¾Ý´æ´¢ÔÚÄÄÀ¹Øϵµ½´úÂëÔËÐÐÆÚ¼äÊý¾Ý±»¼ìË÷µ½µÄËٶȡ£ÔÚJavaScriptÖУ¬´ËÎÊÌâÏà¶Ô¼òµ¥£¬ÒòΪÊý¾Ý´æ´¢Ö»ÓÐÉÙÁ¿·½Ê½¿É¹©Ñ¡Ôñ¡£ÕýÈçÆäËûÓïÑÔÄÇÑù£¬Êý¾Ý´æ´¢Î»ÖùØϵµ½·ÃÎÊËٶȡ£ÔÚJavaScriptÖÐÓÐËÄÖÖ»ù±¾µÄÊý¾Ý·ÃÎÊλÖãº
Literal values Ö±½ÓÁ¿
Any value that represents just itself and isn't stored in a particular location. JavaScript can represent strings, numbers, Booleans, objects, arrays, functions, regular expressions, and the special values null and undefined as literals.
Ö±½ÓÁ¿½ö½ö´ú±í×Ô¼º£¬¶ø²»´æ´¢ÓÚÌض¨Î»ÖᣠJavaScriptµÄÖ±½ÓÁ¿°üÀ¨£º×Ö·û´®£¬Êý×Ö£¬²¼¶ûÖµ£¬¶ÔÏó£¬Êý×飬º¯Êý£¬ÕýÔò±í´ïʽ£¬¾ßÓÐÌØÊâÒâÒåµÄ¿ÕÖµ£¬ÒÔ¼°Î´¶¨Òå¡£
Variables ±äÁ¿
Any developer-defined location for storing data created by using the var keyword.
¿ª·¢ÈËԱʹÓÃvar¹Ø¼ü×Ö´´½¨ÓÃÓÚ´æ´¢Êý¾ÝÖµ¡£
Array items Êý×éÏî
A numerically indexed location within a JavaScript Array object.
¾ßÓÐÊý×ÖË÷Òý£¬´æ´¢Ò»¸öJavaScriptÊý×é¶ÔÏó¡£
Object members ¶ÔÏó³ÉÔ±
A string-indexed location within a JavaScript object.
¾ßÓÐ×Ö·û´®Ë÷Òý£¬´æ´¢Ò»¸öJavaScript¶ÔÏó¡£
Each of these data storage locations has a particular cost associated with reading and writing operations involving the data. In most cases, the performance difference between accessing information from a literal value versus a local variable is trivial. Accessing info
Ïà¹ØÎĵµ£º
¿´ÊéµÄʱºòÓöµ½ÕâÑùÒ»¸öÎÊÌ⣬³ÌÐò´úÂëÈçÏÂ
var ob = function(){
var obj = this;
function fn1(){
alert( obj === window );//false
alert( this === window );//ture
}
this.fn2 = function() {
fn1();
}
}
µ±Ê±ºÜ²»Ã÷°×fn1ÀïÃæµÚ¶þ¸öalertµÄ½á¹û£¬Îª ......
<SCRIPT>
//ÅжÏyÄêµÄÅ©ÀúÖÐÄǸöÔÂÊÇÈòÔÂ,²»ÊÇÈòÔ·µ»Ø0
function leapMonth(y){
return(lunarInfo[y-1900]&0xf);
}
var lunarInfo=new Array(
0x04bd8,0x04ae0,0x0a570,0x054d5,0x0d260,0x0d950,0x16554,0x056a0,0x09ad0,0x055d2,
0x04ae0,0x0a5b6,0x0a4d0,0x0d250,0x1d255,0x0b540,0x0d6a0,0x0ada2,0 ......
JavascriptÖеļ̳лúÖÆÊÇËùνµÄÔÐͶÔÏó¼Ì³Ð£¬Í¨¹ýÊôÐÔ·ÃÎÊ»úÖƵÄÌØÊâÐÔÀ´ÊµÏּ̳еġ£Ö®Ç°ÎÒÒ»Ö±ÒÔΪËùÓжÔÏóµÄÔÐͶÔÏó¾ÍÊÇprototypeËùÖ¸µÄÄǸö¶ÔÏ󡣺óÀ´ÔÚ¿´javascriptÉè¼ÆģʽµÄʱºò·¢ÏÖ×÷Õß×¢ÖÐÓÐÒ»¾ä»°ÓëÎÒ֮ǰµÄÀí½â²»Í¬£¬Ëû˵“ÿ¸ö¶ÔÏó¶¼ÓÐÒ»¸öÔÐͶÔÏ󣬵«Õâ²¢²»Òâζ×Åÿ¸ö¶ÔÏó¶ ......
Grouping Scripts ³É×é½Å±¾
Since each <script> tag blocks the page from rendering during initial download, it's helpful to limit the total number of <script> tags contained in the page. This applies to both inline scripts as well as those in external files. Every time ......