文章分类

站点统计

  • 分类总数: 13 个
  • 文章总数: 145 篇
  • 评论总数: 35 条
  • 附件总数: 59 个
  • 建站日期: 2008-08-18
  • 访问总数: 388498 人次
  • RSS订阅: 文章|评论

JavaScript下实现StringBuilder

Admin 于 2008-09-17 11:01:15 发表JavaScript

订阅: http://www.kaiyuan8.org/Feed/Article_64.aspx
引用: 点这里获取地址 (UTF-8)
通过PostSharp解决NHibernate实体类属性需要加virtual关键字的问题 < JavaScript下实现StringBuilder > 开源Asp.net Wiki系统:ScrewTurn Wiki

自从Web2.0网站流行后,浏览器比以前承担了更多的计算,但是在IE上进行字符串连接非常慢,如果需要进行较多的字符串连接就非常有必要使用StringBuilder进行高速地字符串的构建。以下是JavaScript实现的StringBuilder:

  1. // Initializes a new instance of the StringBuilder class 
  2. // and appends the given value if supplied 
  3. function StringBuilder(value){ 
  4.     this.strings = new Array(""); 
  5.     this.append(value); 
  6.  
  7. // Appends the given value to the end of this instance. 
  8. StringBuilder.prototype.append = function (value){ 
  9.     if (value) this.strings.push(value); 
  10.     return this
  11.  
  12. // Clears the string buffer 
  13. StringBuilder.prototype.clear = function (){ 
  14.     this.strings.length = 1; 
  15.  
  16. // Converts this instance to a String. 
  17. StringBuilder.prototype.toString = function (delimiter){ 
  18.     delimiter = delimiter || ''
  19.     return this.strings.join(delimiter); 
  20.  
  21. //Storing the RegExp as a constant    
  22. //to avoid initializing a new instance at each call. 
  23. StringBuilder.prototype.CompositeFormattingRegExp = new RegExp(/{\d{1,}}/); 
  24.  
  25. StringBuilder.prototype.appendFormat = function() { 
  26.  
  27.     //The first argument is the composite string 
  28.     var Result = arguments[0]; 
  29.  
  30.     //Looping through the composite string 
  31.     //replacing each {n} with the arguments[n+1] value 
  32.     var RegExpResult = null
  33.     while ((RegExpResult = StringBuilder.prototype.CompositeFormattingRegExp.exec(Result)) != null) { 
  34.  
  35.         //RegExp.exec doesn't return a string, type conversion is necessary 
  36.         RegExpResult = RegExpResult.toString(); 
  37.          
  38.         //Extracting the index, 
  39.         //and converting it to a Number, since we're dealing with a string 
  40.         var paramIndex = new Number(RegExpResult.substr(1, RegExpResult.length-2)) 
  41.  
  42.         //arguments[0] is the composite string 
  43.         //arguments[1] is the first argument 
  44.         //adding 1 to paramIndex makes sense then... 
  45.         Result = Result.replace(RegExpResult, arguments[paramIndex + 1]); 
  46.     } 
  47.  
  48.     // 
  49.     this.append(Result); 
  50.     return this

测试页面代码:

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> 
  2. <html> 
  3.     <head> 
  4.         <title>JavaScript StringBuilder</title> 
  5.         <script src="StringBuilder.js" type="text/javascript"></script> 
  6.         <script type="text/javascript"> 
  7.         <!-- 
  8.             function runTests() 
  9.             { 
  10.                 // string concat test 
  11.                 var s = new String(); 
  12.                  
  13.                 var startTime = new Date(); 
  14.                  
  15.                 for (var i = 1; i < 5001; i++) 
  16.                 { 
  17.                     s += i + " Lorem ipsum dolor 中文sit amet, consectetuer adipiscing elit, sed diem nonummy nibh euismod tincidunt ut lacreet dolore magna aliguam erat volutpat.<br>"; 
  18.                 } 
  19.                  
  20.                 var concatResult = s; 
  21.                  
  22.                 var endTime = new Date(); 
  23.                 var concatTime = endTime - startTime; 
  24.                  
  25.                 // StringBuilder test 
  26.                 var sb = new StringBuilder(); 
  27.                  
  28.                 startTime = new Date(); 
  29.                  
  30.                 for (var i = 1; i < 5001; i++) 
  31.                 { 
  32.                     sb.append(i).appendFormat(" Lorem ipsum dolor {0}sit amet, consectetuer adipiscing elit, sed diem nonummy nibh euismod tincidunt ut lacreet dolore magna aliguam erat volutpat.<br>",'中文'); 
  33.                 } 
  34.                  
  35.                 var sbsbResult = sb.toString(); 
  36.                  
  37.                 endTime = new Date(); 
  38.                 var sbTime = endTime - startTime; 
  39.                  
  40.                 document.writeln("String concat test complete.  Total process time <b>" + concatTime + "</b> ms.<br>"); 
  41.                 document.writeln("StringBuilder test complete.  Total process time <b>" + sbTime + "</b> ms.<br>"); 
  42.                 document.writeln("Resulting strings are " + ((concatResult == sbResult) ? "" : "NOT ") + "identical.<br>"); 
  43.             } 
  44.              
  45.             function button_click() 
  46.             { 
  47.                 document.body.innerHTML = "Running tests..."
  48.                 window.setTimeout(runTests, 0); 
  49.             } 
  50.         //--> 
  51.         </script> 
  52.     </head> 
  53.     <body> 
  54.         <form> 
  55.             <input type="button" value="Click me to run tests" onclick="button_click();"> 
  56.         </form> 
  57.     </body> 
  58. </html> 

 

被阅767次, 0投一票

Ben

2009-02-15 10:37:11
好像要超过一千次的连接字符串才看到优势. 1001次连接的结果是: String concat test complete. Total process time 58 ms. StringBuilder test complete. Total process time 56 ms. 不是很明显,但是升到2001, String concat 就开始犯病了!!
  • 看完了要说点啥么?
  • 昵称 (不填说不了话)
  • 信箱地址 (不会被公开,但是不填也说不了话)
  • 网址 (这个不填也成)
Powered by MiniBoke v2.0.0.8 Build 0828

Copyright © 2008 开源吧!. All rights reserved.

粤ICP备07500939号