[·Òë]High Performance JavaScript(012)
µÚËÄÕ Algorithms and Flow Control Ëã·¨ºÍÁ÷³Ì¿ØÖÆ
The overall structure of your code is one of the main determinants as to how fast it will execute. Having a very small amount of code doesn't necessarily mean that it will run quickly, and having a large amount of code doesn't necessarily mean that it will run slowly. A lot of the performance impact is directly related to how the code has been organized and how you're attempting to solve a given problem.
´úÂëÕûÌå½á¹¹ÊÇÖ´ÐÐËٶȵľö¶¨ÒòËØÖ®Ò»¡£´úÂëÁ¿ÉÙ²»Ò»¶¨ÔËÐÐËٶȿ죬´úÂëÁ¿¶àÒ²²»Ò»¶¨ÔËÐÐËÙ¶ÈÂý¡£ÐÔÄÜËðʧÓë´úÂë×éÖ¯·½Ê½ºÍ¾ßÌåÎÊÌâ½â¾ö°ì·¨Ö±½ÓÏà¹Ø¡£
The techniques in this chapter aren't necessarily unique to JavaScript and are often taught as performance optimizations for other languages. There are some deviations from advice given for other languages, though, as there are many more JavaScript engines to deal with and their quirks need to be considered, but all of the techniques are based on prevailing computer science knowledge.
±¾Õ¼¼Êõ²»½öÊÊÓÃÓÚJavaScriptÒ²ÊÊÓÃÓÚÆäËûÓïÑÔµÄÐÔÄÜÓÅ»¯¡£»¹ÓÐһЩΪÆäËûÓïÑÔÌṩµÄ½¨Ò飬»¹Òª´¦Àí¶àÖÖJavaScriptÒýÇæ²¢¿¼ÂÇËüÃǵIJîÒ죬µ«ÕâЩ¼¼Êõ¶¼ÒÔµ±Ç°¼ÆËã»ú¿Æѧ֪ʶΪ»ù´¡¡£
Loops Ñ»·
In most programming languages, the majority of code execution time is spent within loops. Looping over a series of values is one of the most frequently used patterns in programming and as such is also one of the areas where efforts to improve performance must be focused. Understanding the performance impact of loops in JavaScript is especially important, as infinite or long-running loops severely impact the overall user experience.
ÔÚ´ó¶àÊý±à³ÌÓïÑÔÖУ¬´úÂëÖ´ÐÐʱ¼ä¶àÊýÔÚÑ»·Öжȹý¡£ÔÚһϵÁбà³ÌģʽÖУ¬Ñ»·ÊÇ×î³£ÓõÄģʽ֮һ£¬Òò´ËÒ²ÊÇÌá¸ßÐÔÄܱØÐë¹Ø×¢µÄµØÇøÖ®Ò»¡£Àí½âJavaScriptÖÐÑ»·¶ÔÐÔÄܵÄÓ°ÏìÖÁ¹ØÖØÒª£¬ÒòΪËÀÑ»·»òÕß³¤Ê±¼äÔËÐеÄÑ»·»áÑÏÖØÓ°ÏìÓû§ÌåÑé¡£
Types of Loops Ñ»·µÄÀàÐ
Ïà¹ØÎĵµ£º
µÚÈýÕ DOM Scripting DOM±à³Ì
DOM scripting is expensive, and it's a common performance bottleneck in rich web applications. This chapter discusses the areas of DOM scripting that can have a negative effect on an application's responsiveness and gives recommendations o ......
Cloning Nodes ½Úµã¿Ë¡
Another way of updating page contents using DOM methods is to clone existing DOM elements instead of creating new ones—in other words, using element.cloneNode() (where element is an existing node) instead of document.createElement().
&nbs ......
JavaScriptʼþ´óÈ«
click() ¶ÔÏó.click() ʹ¶ÔÏó±»µã»÷¡£
closed ¶ÔÏó.closed ¶ÔÏó´°¿ÚÊÇ·ñÒѹرÕtrue/false
clearTimeout(¶ÔÏó) Çå³ýÒÑÉèÖõÄsetTimeout¶ÔÏó
clearInterval(¶ÔÏó) Çå³ýÒÑÉèÖõÄsetInterval¶ÔÏó
confirm("ÌáʾÐÅÏ¢") µ¯³öÈ·ÈÏ¿ò£¬È·¶¨·µ»ØtrueÈ¡Ïû·µ»Øfalse
cursor:Ñùʽ ¸ü¸ÄÊó±êÑùÊ ......
AJAX (Òì²½ JavaScript ºÍ XML) ÊǸöвúÉúµÄÊõÓï,רΪÃèÊöJavaScriptµÄÁ½ÏîÇ¿´óÐÔÄÜ.ÕâÁ½ÏîÐÔ
ÄÜÔÚ¶àÄêÀ´Ò»Ö±±»ÍøÂ翪·¢ÕßËùºöÂÔ,Ö±µ½×î½üGmail, Google suggestºÍgoogle MapsµÄºá¿Õ³öÊÀ²ÅʹÈË
ÃÇ¿ªÊ¼Òâʶµ½ÆäÖØÒªÐÔ.
ÕâÁ½Ïî±»ºöÊÓµÄÐÔÄÜÊÇ:
* ÎÞÐèÖØÐÂ×°ÔØÕû¸öÒ³Ãæ±ãÄÜÏò·þÎñÆ÷·¢ËÍÇëÇó.
* ¶ÔXMLÎĵµµÄ½âÎöºÍ´¦Àí£®
......