[·Òë]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
Ïà¹ØÎĵµ£º
±¾ÎÄÖ÷ÒªÊdzöÓÚÓÐÅóÓÑʹÓÃÎÒÔÀ´Ð´µÄautocompleteµÄJS¿Ø¼þ¡£µ±Êý¾ÝÁ¿´óµÄʱºò£¬»á³öÏÖЧÂʼ«ÆäÂýµÄÇé¿ö£¬ÎÒÔÚÕâ¶Îʱ¼ä×ö³öµÄһЩ²âÊÔÒ²¼°Ò»Ð©¾Ñ飬Óë´ó¼Ò·ÖÏí£¬Èç¹ûÓдíµÄµØ·½£¬»¹ÇëÖ¸³ö¡£
¾¹ý²âÊÔ£¬ÎÒÃǻᷢÏÖÈçϵÄÇé¿ö»òÕß˵µÄ½áÂÛ£¬Èç¹ûÄúµÄ²âÊÔ½á¹ûÓëÎҵIJ»·û£¬Çë˵Ã÷ÔÒò£¬ÒÔ±ãÏ໥ѧϰ¡£
1£©µ±Ò»¸ö½Ï´óµÄHTML×Ö· ......
calcu() ----º¯ÊýʵÏÖÁ½¸öÊýÏà³Ë
document.write(); ---´òÓ¡
document.calc.ÔªËØÃû.value; --»ñµÃ±íµ¥µÄÖµ
document.±íµ¥Ãû¡£±íµ¥ÔªËØÃû¡£value Ϊ±íµ¥¸³Öµ
¶¨Ò庯Êý£º
function Ãû×Ö()
{
//javaScriptÔ¤Óï¾ä
}
onClick="º¯ÊýÃû";  ......
Dynamic Script Elements ¶¯Ì¬½Å±¾ÔªËØ
The Document Object Model (DOM) allows you to dynamically create almost any part of an HTML document using JavaScript. At its root, the <script> element isn't any different than any other element on a page: references can be retrie ......
XMLHttpRequest Script Injection XHR½Å±¾×¢Èë
Another approach to nonblocking scripts is to retrieve the JavaScript code using an XMLHttpRequest (XHR) object and then inject the script into the page. This technique involves creating an XHR object, downloading the JavaScript f ......