---
title: "JavaScript中，String和StringBuffer性能PK"
author: "zwfc"
date: "2008-12-25T13:35:00Z"
tags: []
slug: "javascript-string-stringbuffer-pk"
draft: false
lastmod: "2008-12-25T13:35:00Z"
id: 470943
url: "https://zwfc.iteye.com/blog/470943"
---

01. functionStringBuffer(){
02. this.\_strings=newArray;
03. }
04. StringBuffer.prototype.append=function(str){
05. this.\_strings.push(str);
06. };
07. StringBuffer.prototype.toString=function(){
08. returnthis.\_strings.join(“”);
09. };
10. vard1=newDate();
11. varstr=“”;
12. for(vari=0;i&lt;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&lt;10000;i++){
21. oBuffer.append(“text”);
22. }
23. varsResult=buffer.toString();
24. d2=newDate();
25. document.write(“&lt;br/&gt;ConcatenationwithStringBuffer:“+(d2.getTime()-
26. d1.getTime())+“milliseconds”);