ITeye博客

JavaScript中,String和StringBuffer性能PK

zwfc · 2008-12-25 13:35 · Markdown

  1. functionStringBuffer(){
  2. this._strings=newArray;
  3. }
  4. StringBuffer.prototype.append=function(str){
  5. this._strings.push(str);
  6. };
  7. StringBuffer.prototype.toString=function(){
  8. returnthis._strings.join(“”);
  9. };
  10. vard1=newDate();
  11. varstr=“”;
  12. for(vari=0;i<10000;i++){
  13. str+=“text”;
  14. }
  15. vard2=newDate();
  16. document.write(“Concatenationwithplus:“+(d2.getTime()-d1.getTime())+“
  17. milliseconds”);
  18. varoBuffer=newStringBuffer();
  19. d1=newDate();
  20. for(vari=0;i<10000;i++){
  21. oBuffer.append(“text”);
  22. }
  23. varsResult=buffer.toString();
  24. d2=newDate();
  25. document.write(“<br/>ConcatenationwithStringBuffer:“+(d2.getTime()-
  26. d1.getTime())+“milliseconds”);

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。