[·Òë]High Performance JavaScript(022)
µÚÆßÕ Ajax Òì²½JavaScriptºÍXML
Ajax is a cornerstone of high-performance JavaScript. It can be used to make a page load faster by delaying the download of large resources. It can prevent page loads altogether by allowing for data to be transferred between the client and the server asynchronously. It can even be used to fetch all of a page's resources in one HTTP request. By choosing the correct transmission technique and the most efficient data format, you can significantly improve how your users interact with your site.
AjaxÊǸßÐÔÄÜJavaScriptµÄ»ùʯ¡£Ëü¿ÉÒÔͨ¹ýÑÓ³ÙÏÂÔØ´óÁ¿×ÊԴʹҳÃæ¼ÓÔظü¿ì¡£Ëüͨ¹ýÔÚ¿Í»§¶ËºÍ·þÎñÆ÷Ö®¼äÒì²½´«ËÍÊý¾Ý£¬±ÜÃâÒ³Ã漯Ìå¼ÓÔØ¡£Ëü»¹ÓÃÓÚÔÚÒ»´ÎHTTPÇëÇóÖлñÈ¡Õû¸öÒ³ÃæµÄ×ÊÔ´¡£Í¨¹ýÑ¡ÔñÕýÈ·µÄ´«Êä¼¼ÊõºÍ×îÓÐЧµÄÊý¾Ý¸ñʽ£¬Äã¿ÉÒÔÏÔÖø¸ÄÉÆÓû§ÓëÍøÕ¾Ö®¼äµÄ»¥¶¯¡£
This chapter examines the fastest techniques for sending data to and receiving it from the server, as well as the most efficient formats for encoding data.
±¾Õ¿¼²ì´Ó·þÎñÆ÷ÊÕ·¢Êý¾Ý×î¿ìµÄ¼¼Êõ£¬ÒÔ¼°×îÓÐЧµÄÊý¾Ý±àÂë¸ñʽ¡£
Data Transmission Êý¾Ý´«Êä
Ajax, at its most basic level, is a way of communicating with a server without unloading the current page; data can be requested from the server or sent to it. There are several different ways of setting up this communication channel, each with its own advantages and restrictions. This section briefly examines the different approaches and discusses the performance implications of each.
Ajax£¬ÔÚËü×î»ù±¾µÄ²ãÃ棬ÊÇÒ»ÖÖÓë·þÎñÆ÷ͨѶ¶ø²»ÖØÔص±Ç°Ò³ÃæµÄ·½·¨£¬Êý¾Ý¿É´Ó·þÎñÆ÷»ñµÃ»ò·¢Ë͸ø·þÎñÆ÷¡£ÓжàÖÖ²»Í¬µÄ·½·¨¹¹ÔìÕâÖÖͨѶͨµÀ£¬Ã¿ÖÖ·½·¨¶¼ÓÐ×Ô¼ºµÄÓÅÊƺÍÏÞÖÆ¡£±¾½Ú¼òÒªµØ½éÉÜÕâЩ²»Í¬·½·¨£¬²¢ÌÖÂÛ¸÷×Ô¶ÔÐÔÄܵÄÓ°Ïì¡£
Requesting Data ÇëÇóÊý¾Ý
There are five general techniques for requesting data from a server:
ÓÐÎåÖÖ³£Óü¼ÊõÓÃÓÚÏò·þÎñÆ÷ÇëÇóÊý¾Ý£º
• XMLHttpRequest (XHR)
• Dynamic scrip
Ïà¹ØÎĵµ£º
Ò³ÃæÌá½»Êý¾ÝÒ»°ãÓÐÁ½ÖÖ·½·¨£ºget,post¡£post¾ÍÊÇËùνµÄformÌá½»£¬Ê¹ÓÃÊÓͼ£»getÊÇͨ¹ýurlÌá½»¡£
Get·½·¨Ò»°ãÓúǫ́´úÂ루Èçasp,asp.net£©»ñµÃ²ÎÊý£¬´úÂëºÜ¼òµ¥£ºRequest.QueryString["id"];¼´¿É»ñÈ¡¡£
ÓÐЩʱºòÐèÒªÖ±½ÓÔÚǰ̨»ñÈ¡url²ÎÊý£¬ÒªÓõ½javascript£¬jsûÓÐÖ±½Ó»ñÈ¡url²ÎÊýµÄ·½·¨£¬ÄÇô£¬ÎÒÃÇÈçºÎͨ¹ýjs ......
JavascriptÊý¾ÝÀàÐÍ
ÓÉÓÚjavascriptÊÇÈõÀàÐÍÓïÑÔ£¬¼´¶¨Òå±äÁ¿Ê±²»±ØÉùÃ÷ÆäÀàÐÍ
¡£µ«Õâ²¢²»Òâζ×űäÁ¿Ã»ÓÐÀàÐÍ¡£ÒòΪ¸³ÖµÊ±»á×Ô¶¯Æ¥ÅäÊý¾ÝÀàÐÍ£¡
Ä¿Ç°Óõ½µÄ»ù±¾Êý¾ÝÀàÐÍ
number
boolean
string
var i
i="test";//Õâʱi¾Í³ÉÁËstringÀàÐÍ
var i
i=4;//Õâʱi¾Í³ÉÁËnumberÀàÐÍ
³£ÓöÔÏóÀàÐÍ<object> ......
[ת×Ô]http://article.yeeyan.org/view/mouse4x/16540
JSON±»¹«ÈÏΪä¯ÀÀÆ÷ÖÐXMLµÄºó¼ÌÕߣ¬ËüµÄÄ¿±ê½ö½öÊdzÉΪһÖÖ¼òµ¥¡¢ÓÅÑŵÄÊý¾Ý¸ñʽ£¬ÒÔ·½±ãä¯ÀÀÆ÷ºÍ·þÎñÆ÷Ö®¼äµÄÊý¾Ý½»»»¡£ÔÚÍê³ÉÕâÒ»¼òµ¥ÈÎÎñµÄ¹ý³ÌÖУ¬Ëü½«ÒýÁìÏÂÒ»´úÍòάÍø¡£
¶ÔÏó¼ò½é
¿´,Õâ¾ÍÊÇÒ»¸ö¶ÔÏó:
var myFirstObject ={};
¾¡¹Ü¿´ÆðÀ´Í¦¼òµ¥£¬È»¶øÄÇЩ» ......
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 ......
Regular Expression Optimization ÕýÔò±í´ïʽÓÅ»¯
Incautiously crafted regexes can be a major performance bottleneck (the upcoming section, "Runaway Backtracking" on page 91, contains several examples showing how severe this can be), but there is a lot you can do to improve re ......