javascript stringbuilder

2008. 11. 17. 18:41 개발언어/ASP.NET

javascript로 문자열 합치는 방법인데 상속받아서 쓰면 속도가 차이가 난다.

전에 훈스 세미나갔을대 본것이 기억난다.

그냥 변수 + 변수 하는것보다 빠르단다.

코드프로젝트의 들어가는 말이다.


Once upon a time, not so long ago, pushing any significant processing to the client browser was considered a bad practice. Now with the rise in popularity of AJAX style development, it has suddenly become a hot new technology. Unfortunately, the most commonly used browser on the market is painfully slow at one of the most common tasks in programming -- string concatenation.

The good news is that although IE is slow when it comes to string concatenation, it is quite fast with array operations. With this in mind, I decided to write a simple StringBuilder class that pushes individual strings into an array and then uses the join method to produce the concatenated output string. In tests I have run with 5,000 strings, it is 117 times faster than the equivalent string concatenation using the s1 += s2 syntax. With 10,000 strings, it is an amazing 261

아래 링크에 잘 나와있다.

// Initializes a new instance of the StringBuilder class
// and appends the given value if supplied
function StringBuilder(value)
    this.strings = new Array("");

// Appends the given value to the end of this instance.
StringBuilder.prototype.append = function (value)
    if (value)

// Clears the string buffer
StringBuilder.prototype.clear = function ()
    this.strings.length = 1;

// Converts this instance to a String.
StringBuilder.prototype.toString = function ()
    return this.strings.join("");
/* 사용법
// append some text
sb.append("Lorem ipsum dolor sit amet, consectetuer adipiscing elit, ");
sb.append("sed diem nonummy nibh euismod tincidunt ut lacreet dolore ");
sb.append("magna aliguam erat volutpat.");