[·Òë]High Performance JavaScript(023)
Data Formats Êý¾Ý¸ñʽ
When considering data transmission techniques, you must take into account several factors: feature set, compatibility, performance, and direction (to or from the server). When considering data formats, the only scale you need for comparison is speed.
ÔÚ¿¼ÂÇÊý¾Ý´«Êä¼¼Êõʱ£¬Äã±ØÐ뿼ÂÇÕâЩÒòËØ£º¹¦Äܼ¯£¬¼æÈÝÐÔ£¬ÐÔÄÜ£¬ºÍ·½Ïò£¨·¢¸ø·þÎñÆ÷»òÕß´Ó·þÎñÆ÷½ÓÊÕ£©¡£ÔÚ¿¼ÂÇÊý¾Ý¸ñʽʱ£¬Î¨Ò»ÐèÒª±È½ÏµÄ³ß¶ÈµÄ¾ÍÊÇËٶȡ£
There isn't one data format that will always be better than the others. Depending on what data is being transferred and its intended use on the page, one might be faster to download, while another might be faster to parse. In this section, we create a widget for searching among users and implement it using each of the four major categories of data formats. This will require us to format a list of users on the server, pass it back to the browser, parse that list into a native JavaScript data structure, and search it for a given string. Each of the data formats will be compared based on the file size of the list, the speed of parsing it, and the ease with which it's formed on the server.
ûÓÐÄÄÖÖÊý¾Ý¸ñʽ»áʼÖÕ±ÈÆäËû¸ñʽ¸üºÃ¡£¸ù¾Ý´«ËÍʲôÊý¾Ý¡¢ÓÃÓÚÒ³ÃæÉÏʲôĿµÄ£¬Ä³ÖÖ¸ñʽ¿ÉÄÜÏÂÔظü¿ì£¬ÁíÒ»ÖÖ¸ñʽ¿ÉÄܽâÎö¸ü¿ì¡£ÔÚ±¾½ÚÖУ¬ÎÒÃÇ´´½¨ÁËÒ»¸ö´°¿ÚС²¿¼þÓÃÓÚËÑË÷Óû§ÐÅÏ¢²¢ÓÃËÄÖÖÖ÷Á÷µÄÊý¾Ý¸ñʽʵÏÖËü¡£ÕâÒªÇóÎÒÃÇÔÚ·þÎñÆ÷¶Ë¸ñʽ»¯Ò»¸öÓû§ÁÐ±í£¬½«Ëü·µ»Ø¸øä¯ÀÀÆ÷£¬½«Áбí½âÎö³ÉJavaScriptÊý¾Ý¸ñʽ£¬²¢ËÑË÷Ìض¨µÄ×Ö·û´®¡£Ã¿ÖÖÊý¾Ý¸ñʽ½«±È½ÏÁбíµÄÎļþ´óС£¬½âÎöËٶȣ¬ºÍ·þÎñÆ÷ÉϹ¹ÔìËüÃǵÄÄÑÒ׳̶ȡ£
XML
When Ajax first became popular, XML was the data format of choice. It had many things going for it: extreme interoperability (with excellent support on both the server side and the client side), strict formatting, and easy validation. JSON hadn't been formalized yet as an interchange format, and almost every language used on servers had a library available for working with XML.
&
Ïà¹ØÎĵµ£º
±¾ÎÄÖ÷ÒªÊdzöÓÚÓÐÅóÓÑʹÓÃÎÒÔÀ´Ð´µÄautocompleteµÄJS¿Ø¼þ¡£µ±Êý¾ÝÁ¿´óµÄʱºò£¬»á³öÏÖЧÂʼ«ÆäÂýµÄÇé¿ö£¬ÎÒÔÚÕâ¶Îʱ¼ä×ö³öµÄһЩ²âÊÔÒ²¼°Ò»Ð©¾Ñ飬Óë´ó¼Ò·ÖÏí£¬Èç¹ûÓдíµÄµØ·½£¬»¹ÇëÖ¸³ö¡£
¾¹ý²âÊÔ£¬ÎÒÃǻᷢÏÖÈçϵÄÇé¿ö»òÕß˵µÄ½áÂÛ£¬Èç¹ûÄúµÄ²âÊÔ½á¹ûÓëÎҵIJ»·û£¬Çë˵Ã÷ÔÒò£¬ÒÔ±ãÏ໥ѧϰ¡£
1£©µ±Ò»¸ö½Ï´óµÄHTML×Ö· ......
£¼script language="javaScript"£¾
function closeWindow()
{
¡¡window.opener = null;
¡¡window.open(' ', '_self', ' ');
¡¡window.close();
}
£¼/script£¾
£¼input type='button' value='¹Ø±Õ´°¿Ú' onClick="closeWindow()"£¾
»ò
£¼input type="button" value="¹ ......
ʹÓÃ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( ......
String Trimming ×Ö·û´®ÐÞ¼ô
Removing leading and trailing whitespace from a string is a simple but common task. Although ECMAScript 5 adds a native string trim method (and you should therefore start to see this method in upcoming browsers), JavaScript has not historically in ......
Yielding with Timers Óö¨Ê±Æ÷Èóöʱ¼äƬ
Despite your best efforts, there will be times when a JavaScript task cannot be completed in 100 milliseconds or less because of its complexity. In these cases, it's ideal to yield control of the UI thread so that UI updates may occur ......