[·Òë]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λÊýÉÏÊ
Ïà¹ØÎĵµ£º
Òþ²Ø³ÉÔ±±äÁ¿
ÔÚº¯ÊýÌåÄÚ¶¨ÒåµÄ±äÁ¿Îª¾Ö²¿±äÁ¿£¬À뿪º¯Êý¾Í¹ÒµôÁË
ÔÚº¯ÊýÌåÄÚʹÓÃthis.³ÉÔ±±äÁ¿Ãû£¬ÔòΪwindow¶ÔÏ󼶱äÁ¿£¬¼´È«¾Ö±äÁ¿
¹ÊÐèÒªÕâÑùÒþ²Ø³ÉÔ±±äÁ¿£¬ÏòÍâÖ»±©Â¶get¡¢setº¯Êý
function testClass(name){
var _firstname=name;
return {
getname : function() {
return _fir ......
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 ......
µÚÎåÕ Strings and Regular Expressions ×Ö·û´®ºÍÕýÔò±í´ïʽ
Practically all JavaScript programs are intimately tied to strings. For example, many applications use Ajax to fetch strings from a server, convert those strings into more easily usable JavaScript objects, and ......
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 ......