关注开源代码的实际应用
自从Web2.0网站流行后,浏览器比以前承担了更多的计算,但是在IE上进行字符串连接非常慢,如果需要进行较多的字符串连接就非常有必要使用StringBuilder进行高速地字符串的构建。以下是JavaScript实现的StringBuilder:
- // Initializes a new instance of the StringBuilder class
- // and appends the given value if supplied
- function StringBuilder(value){
- this.strings = new Array("");
- this.append(value);
- }
- // Appends the given value to the end of this instance.
- StringBuilder.prototype.append = function (value){
- if (value) this.strings.push(value);
- return this;
- }
- // Clears the string buffer
- StringBuilder.prototype.clear = function (){
- this.strings.length = 1;
- }
- // Converts this instance to a String.
- StringBuilder.prototype.toString = function (delimiter){
- delimiter = delimiter || '';
- return this.strings.join(delimiter);
- }
- //Storing the RegExp as a constant
- //to avoid initializing a new instance at each call.
- StringBuilder.prototype.CompositeFormattingRegExp = new RegExp(/{\d{1,}}/);
- StringBuilder.prototype.appendFormat = function() {
- //The first argument is the composite string
- var Result = arguments[0];
- //Looping through the composite string
- //replacing each {n} with the arguments[n+1] value
- var RegExpResult = null;
- while ((RegExpResult = StringBuilder.prototype.CompositeFormattingRegExp.exec(Result)) != null) {
- //RegExp.exec doesn't return a string, type conversion is necessary
- RegExpResult = RegExpResult.toString();
- //Extracting the index,
- //and converting it to a Number, since we're dealing with a string
- var paramIndex = new Number(RegExpResult.substr(1, RegExpResult.length-2))
- //arguments[0] is the composite string
- //arguments[1] is the first argument
- //adding 1 to paramIndex makes sense then...
- Result = Result.replace(RegExpResult, arguments[paramIndex + 1]);
- }
- //
- this.append(Result);
- return this;
- }
测试页面代码:
- <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
- <html>
- <head>
- <title>JavaScript StringBuilder</title>
- <script src="StringBuilder.js" type="text/javascript"></script>
- <script type="text/javascript">
- <!--
- function runTests()
- {
- // string concat test
- var s = new String();
- var startTime = new Date();
- for (var i = 1; i < 5001; i++)
- {
- s += i + " Lorem ipsum dolor 中文sit amet, consectetuer adipiscing elit, sed diem nonummy nibh euismod tincidunt ut lacreet dolore magna aliguam erat volutpat.<br>";
- }
- var concatResult = s;
- var endTime = new Date();
- var concatTime = endTime - startTime;
- // StringBuilder test
- var sb = new StringBuilder();
- startTime = new Date();
- for (var i = 1; i < 5001; i++)
- {
- 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>",'中文');
- }
- var sbsbResult = sb.toString();
- endTime = new Date();
- var sbTime = endTime - startTime;
- document.writeln("String concat test complete. Total process time <b>" + concatTime + "</b> ms.<br>");
- document.writeln("StringBuilder test complete. Total process time <b>" + sbTime + "</b> ms.<br>");
- document.writeln("Resulting strings are " + ((concatResult == sbResult) ? "" : "NOT ") + "identical.<br>");
- }
- function button_click()
- {
- document.body.innerHTML = "Running tests...";
- window.setTimeout(runTests, 0);
- }
- //-->
- </script>
- </head>
- <body>
- <form>
- <input type="button" value="Click me to run tests" onclick="button_click();">
- </form>
- </body>
- </html>