¸ßЧJavaScript ×Ö·û´®Ìæ»»º¯ÊýµÄbenchmark
°æȨÉùÃ÷
£º¿ÉÒÔÈÎÒâתÔØ£¬µ«×ªÔØʱ±ØÐë±êÃ÷Ô×÷Õßcharlee¡¢ÔʼÁ´½Óhttp://tech.idv2.com/2006/11/23/javascript-strreplace-benchmark/
ÒÔ¼°±¾ÉùÃ÷¡£
¸ÐлClearΪÎÒÃÇÌṩÁËÕâ¸öЧÂʸü¸ßµÄ×Ö·û´®Ìæ»»º¯Êý
¡£ÕâÊǾ³£Ê¹ÓõÄHTMLÌØÊâ×Ö·ûÌæ»»º¯Êý£¬¼´½« &¡¢<¡¢>¡¢" µÈº¯ÊýÌæ»»³É &¡¢<¡¢>¡¢"¡£Í¨³£µÄ×÷·¨¶¼ÊÇÁ¬ÐøʹÓÃÊý¸ö replace º¯Êý£¬¶øClear½öʹÓÃÒ»¸ö replace ¾ÍÍê³ÉÁËÌæ»»¡£
ÎÒ¶ÔÕâÁ½¸öº¯Êý×÷ÁËbenchmark£¬·¢ÏÖк¯ÊýµÄЧÂÊԼΪԺ¯ÊýµÄ1.4±¶¡£ËäȻûÓÐÔ¤ÏëÖÐÄÇô¸ßЧÂÊ£¬µ«µ±ÒªÌæ»»µÄ×Ö·û´®¸ü¶àʱ£¬Ð§ÂÊÓ¦¸Ã»áÌá¸ß°É¡£
²âÊÔ·½·¨Îª£¬¶ÔÒ»¸ö2048×Ö½Ú³¤µÄ×Ö·û´®·Ö±ðÖ´ÐÐÁ½¸öº¯Êý100´Î£¬È¡ÆäÖ´ÐÐʱ¼ä¡£ÏÂÃæΪ10´Î²âÊԵĽá¹û¡£toTXT_1 Ϊͨ³£µÄ×÷·¨£¬toTXT_2 ΪClearµÄ·½·¨¡£
toTXT_1(ms)
toTXT_2(ms)
toTXT_1/toTXT_2
1609
1204
75%
2250
1547
69%
1672
1313
79%
1813
1250
69%
1844
1219
66%
2063
1421
69%
2031
1172
58%
1797
1360
76%
2015
1281
64%
1860
1500
81%
²âÊÔ´úÂ룺
<html>
<head>
<mce:script type="text/javascript"><!--
function toTXT_1(str){
str = str.replace(/\&/g, "& amp;");
str = str.replace(/\>/g, "& gt;");
str = str.replace(/\</g, "& lt;");
str = str.replace(/\"/g, "& quot;");
str = str.replace(/\'/g, "& #39;");
return str;
}
function toTXT_2(str){
var RexStr = /\<|\>|\"|\'|\&/g
str = str.replace(RexStr,
function(MatchStr){
switch(MatchStr){
case "<":
return "& lt;";
break;
case ">":
return "& gt;";
break;
case "\"":
return "& quot;";
break;
case "'":
return "& #39;";
break;
case "&":
Ïà¹ØÎĵµ£º
Ïȸø³ö»ù±¾µÄHTMLÎļþ£º
<ul>
<li id="m01"></li>
<li id="m01"></li>
<li id="m01"></li>
</ul>
<div>
<div id="C01"> </div>
<div id="C02">&n ......
JavaScript replace() ·½·¨
JavaScript String ¶ÔÏó²Î¿¼ÊÖ²á
¶¨ÒåºÍÓ÷¨
replace() ·½·¨ÓÃÓÚÔÚ×Ö·û´®ÖÐÓÃһЩ×Ö·ûÌæ»»ÁíһЩ×Ö·û£¬»òÌæ»»Ò»¸öÓëÕýÔò±í´ïʽƥÅäµÄ×Ó´®¡£
Óï·¨
stringObject.replace(regexp,replacement)
²ÎÊýÃèÊö
regexp
±ØÐè¡£¹æ¶¨ÁËÒªÌæ»»µÄģʽµÄ RegExp ¶ÔÏó¡£Çë×¢Ò⣬Èç¹û¸ÃÖµÊÇÒ»¸ö×Ö·û´®£¬Ôò½ ......
Ò»¡¢Ê²Ã´ÊDZհü£¿
“¹Ù·½”µÄ½âÊÍÊÇ£ºËùν“±Õ°ü”£¬Ö¸µÄÊÇÒ»¸öÓµÓÐÐí¶à±äÁ¿ºÍ°ó¶¨ÁËÕâЩ±äÁ¿µÄ»·¾³µÄ±í´ïʽ£¨Í¨³£ÊÇÒ»¸öº¯Êý£©£¬Òò¶øÕâЩ±äÁ¿Ò²ÊǸñí´ïʽµÄÒ»²¿·Ö¡£
ÏàÐźÜÉÙÓÐÈËÄÜÖ±½Ó¿´¶®Õâ¾ä»°£¬ÒòΪËûÃèÊöµÄ̫ѧÊõ¡£ÎÒÏëÓÃÈçºÎÔÚJavascriptÖд´½¨Ò»¸ö±Õ°üÀ´¸æËßÄãʲôÊDZհü£¬ÒòΪÌø¹ý±Õ°üµÄ´´ ......
Ò»¡¢ºǫ́£¨.csÎļþ£©·½·¨£º
public string GetString(string name)
{
return ("Hello " + name);
}
&n ......
JavaScriptʼþ·ÖÀࣺ
1¡¢²¶»ñÐÍ£¨IE²»Ö§³Ö£©£¨´ÓDOM½á¹¹µÄ×¶Ë¿ªÊ¼ÏòÏÂÑÓÉ죩
2¡¢Ã°ÅÝÐÍ£¨´ÓDOM½á¹¹µÄ×îµÍ¶ËÏòÉÏÒ»¼¶¼¶ÑÓÉ죩
¡¾IE7ÒÔÉÏ¿ÉÒÔ¸ø‘html’Ìí¼Óonclickʼþ£¬IEÖÐbody->html£¬»ðºüÖÐhtml->body¡¿
JavaScriptÌí¼Óʼþ¼àÌý£º
1¡¢IE£º
[object].attachEvent("event_handler", fnHandler); ......