Ò׽ؽØͼÈí¼þ¡¢µ¥Îļþ¡¢Ãâ°²×°¡¢´¿ÂÌÉ«¡¢½ö160KB

[·­Òë]High Performance JavaScript(021)

Splitting Up Tasks  ·Ö½âÈÎÎñ
    What we typically think of as one task can often be broken down into a series of subtasks. If a single function is taking too long to execute, check to see whether it can be broken down into a series of smaller functions that complete in smaller amounts of time. This is often as simple as considering a single line of code as an atomic task, even though multiple lines of code typically can be grouped together into a single task. Some functions are already easily broken down based on the other functions they call. For example:
    ÎÒÃÇͨ³£½«Ò»¸öÈÎÎñ·Ö½â³ÉһϵÁÐ×ÓÈÎÎñ¡£Èç¹ûÒ»¸öº¯ÊýÔËÐÐʱ¼äÌ«³¤£¬ÄÇô²é¿´ËüÊÇ·ñ¿ÉÒÔ·Ö½â³ÉһϵÁÐÄܹ»¶Ìʱ¼äÍê³ÉµÄ½ÏСµÄº¯Êý¡£¿É½«Ò»ÐдúÂë¼òµ¥µØ¿´×÷Ò»¸öÔ­×ÓÈÎÎñ£¬¶àÐдúÂë×éºÏÔÚÒ»Æð¹¹³ÉÒ»¸ö¶ÀÁ¢ÈÎÎñ¡£Ä³Ð©º¯Êý¿É»ùÓÚº¯Êýµ÷ÓýøÐвð·Ö¡£ÀýÈ磺
function saveDocument(id){
  //save the document
  openDocument(id)
  writeText(id);
  closeDocument(id);
  //update the UI to indicate success
  updateUI(id);
}
    If this function is taking too long, it can easily be split up into a series of smaller steps by breaking out the individual methods into separate timers. You can accomplish this by adding each function into an array and then using a pattern similar to the array-processing pattern from the previous section:
    Èç¹ûº¯ÊýÔËÐÐʱ¼äÌ«³¤£¬Ëü¿ÉÒÔ²ð·Ö³ÉһϵÁиüСµÄ²½Ö裬°Ñ¶ÀÁ¢·½·¨·ÅÔÚ¶¨Ê±Æ÷Öе÷Óá£Äã¿ÉÒÔ½«Ã¿¸öº¯Êý¶¼·ÅÈëÒ»¸öÊý×飬ȻºóʹÓÃÇ°Ò»½ÚÖÐÌáµ½µÄÊý×é´¦Àíģʽ£º
function saveDocument(id){
  var tasks = [openDocument, writeText, closeDocument, updateUI];
  setTimeout(function(){
    //execute the next task
    var task = tasks.shift();
    task(id);
    //determine if there's more
    if (tasks.length > 0){
      setTimeout(arguments.callee, 25);
    }
  }, 25);
}
  


Ïà¹ØÎĵµ£º

ͨ¹ýjavascript»ñµÃurl²ÎÊý

Ò³ÃæÌá½»Êý¾ÝÒ»°ãÓÐÁ½ÖÖ·½·¨£ºget,post¡£post¾ÍÊÇËùνµÄformÌá½»£¬Ê¹ÓÃÊÓͼ£»getÊÇͨ¹ýurlÌá½»¡£
Get·½·¨Ò»°ãÓúǫ́´úÂ루Èçasp,asp.net£©»ñµÃ²ÎÊý£¬´úÂëºÜ¼òµ¥£ºRequest.QueryString["id"];¼´¿É»ñÈ¡¡£ 
ÓÐЩʱºòÐèÒªÖ±½ÓÔÚǰ̨»ñÈ¡url²ÎÊý£¬ÒªÓõ½javascript£¬jsûÓÐÖ±½Ó»ñÈ¡url²ÎÊýµÄ·½·¨£¬ÄÇô£¬ÎÒÃÇÈçºÎͨ¹ýjs ......

javascript¼Ì³Ð·½Ê½Ö®Èý

3¡¢×éºÏ¹¹Ô캯Êý/Ô­ÐÍ·½Ê½Ð´À࣬²ÉÓÃÇ°ÃæÖÖ·½Ê½¼Ì³Ð
ÕâÖÖ·½Ê½¸¸À࣬×ÓÀàµÄÊôÐÔ¶¼¹ÒÔÚ¹¹Ô캯ÊýÀ·½·¨¶¼¹ÒÔÚÔ­ÐÍÉÏ¡£
/**
* ¸¸ÀàPolygon:¶à±ßÐÎ
*/
function Polygon(sides) {
this.sides = sides;
}
Polygon.prototype.setSides = function(s) {this.sides=s;}
/**
* Triangle Èý½ÇÐÎ
* @param {Object} b ......

javaScript¹Ø±Õä¯ÀÀÆ÷ (²»µ¯³öÌáʾ¿ò)

£¼script language="javaScript"£¾
function closeWindow()
{
¡¡window.opener = null;
¡¡window.open(' ', '_self', ' ');
¡¡window.close();
}
£¼/script£¾
£¼input type='button' value='¹Ø±Õ´°¿Ú' onClick="closeWindow()"£¾
»ò
£¼input type="button" value="¹ ......

[·­Òë]High Performance JavaScript(017)

A Note on Benchmarking  ²âÊÔ»ù׼˵Ã÷
    Because a regex's performance can be wildly different depending on the text it's applied to, there's no straightforward way to benchmark regexes against each other. For the best result, you need to benchmark your regexes on test strings o ......

[·­Òë]High Performance JavaScript(018)

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 ......
© 2009 ej38.com All Rights Reserved. ¹ØÓÚE½¡ÍøÁªÏµÎÒÃÇ | Õ¾µãµØͼ | ¸ÓICP±¸09004571ºÅ