[·Òë]High Performance JavaScript(026)
Use the Fast Parts ʹÓÃËٶȿìµÄ²¿·Ö
Even though JavaScript is often blamed for being slow, there are parts of the language that are incredibly fast. This should come as no surprise, since JavaScript engines are built in lower-level languages and are therefore compiled. Though it's easy to blame the engine when JavaScript appears slow, the engine is typically the fastest part of the process; it's your code that is actually running slowly. There are parts of the engine that are much faster than others because they allow you to bypass the slow parts.
ËäÈ»JavaScript¾³£±»Ö¸Ôð»ºÂý£¬È»¶ø´ËÓïÑÔµÄijЩ²¿·Ö¾ßÓÐÄÑÒÔÖÃÐŵĿìËÙ¡£Õâ²»×ãΪÆæÒòΪJavaScriptÒýÇæÓɵͼ¶ÓïÑÔ¹¹½¨¡£ËäÈ»JavaScriptËÙ¶ÈÂýºÜÈÝÒ×±»¹é¾ÌÓÚÒýÇ棬Ȼ¶øÒýÇæͨ³£ÊÇ´¦Àí¹ý³ÌÖÐ×î¿ìµÄ²¿·Ö£¬Êµ¼ÊÉÏËÙ¶ÈÂýµÄÊÇÄãµÄ´úÂë¡£ÒýÇæµÄijЩ²¿·Ö±ÈÆäËü²¿·Ö¿ìºÜ¶à£¬ÒòΪËüÃÇÔÊÐíÄãÈƹýËÙ¶ÈÂýµÄ²¿·Ö¡£
Bitwise Operators λ²Ù×÷ÔËËã·û
Bitwise operators are one of the most frequently misunderstood aspects of JavaScript. General opinion is that developers don't understand how to use these operators and frequently mistake them for their Boolean equivalents. As a result, bitwise operators are used infrequently in JavaScript development, despite their advantages.
λ²Ù×÷ÔËËã·ûÊÇJavaScriptÖо³£±»Îó½âµÄÄÚÈÝÖ®Ò»¡£Ò»°ãµÄ¿´·¨ÊÇ£¬¿ª·¢Õß²»ÖªµÀÈçºÎʹÓÃÕâЩ²Ù×÷·û£¬¾³£ÔÚ²¼¶û±í´ïʽÖÐÎóÓ᣽á¹ûµ¼ÖÂJavaScript¿ª·¢Öв»³£ÓÃλ²Ù×÷ÔËËã·û£¬¾¡¹ÜËüÃǾßÓÐÓÅÊÆ¡£
JavaScript numbers are all stored in IEEE-754 64-bit format. For bitwise operations, though, the number is converted into a signed 32-bit representation. Each operator then works directly on this 32-bit representation to achieve a result. Despite the conversion, this process is incredibly fast when compared to other mathematical and Boolean operations in JavaScript.
JavaScriptÖеÄÊý×Ö°´ÕÕIEEE-754±ê×¼64λ¸ñʽ´æ´¢¡£ÔÚλÔËËãÖУ¬Êý×Ö±»×ª»»ÎªÓзûºÅ32λ¸ñʽ¡£Ã¿ÖÖ²Ù×÷¾ùÖ±½Ó²Ù×÷ÔÚÕâ¸ö32λÊýÉÏÊ
Ïà¹ØÎĵµ£º
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 ......
µÚÁùÕ Responsive Interfaces ÏìÓ¦½Ó¿Ú
There's nothing more frustrating than clicking something on a web page and having nothing happen. This problem goes back to the origin of transactional web applications and resulted in the now-ubiquitous "please click only once" m ......
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 ......
µÚÆßÕ 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 ......
µÚ°ËÕ Programming Practices ±à³Ìʵ¼ù
Every programming language has pain points and inefficient patterns that develop over time. The appearance of these traits occurs as people migrate to the language and start pushing its boundaries. Since 2005, when the term "Ajax" ......