JavaScript:利用StringBuffer类提升+=拼接字符串效率

简介: 1DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 3 4 5 6 7 8 9495051
1 <! DOCTYPE html PUBLIC  " -//W3C//DTD XHTML 1.0 Transitional//EN "  " http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " >
 2 < html xmlns = " http://www.w3.org/1999/xhtml " >
 3 < head >
 4 < meta http - equiv = " Content-Type "  content = " text/html; charset=utf-8 "  / >
 5 < title >< / title>
 6 < / head>
 7 < body >
 8 < / body>
 9 < script type = " text/javascript " ><!--
10      var  str  =  ' hello ' ;
11     str  +=  ' world ' ;
12      // 每次完成字符串连接都会执行步骤2到6步
13      // 实际上,这段代码在幕后执行的步骤如下:
14 img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif      /*
15        1.创建存储'hello'的字符串
16        2.创建存储'world'的字符串
17        3.创建存储链接结果的字符串
18        4.把str的当前内容复制到结果中
19        5.把'world'复制到结果中
20        6.更新str,使它指向结果
21    */
    
22     
23      // 为了提高性能最好使用数组方法拼接字符串
24      // 创建一个StringBuffer类
25 img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif      function  StringBuffer() {
26        this.__strings__ = [];
27    }
;    
28 img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif    StringBuffer.prototype.append  =  function (str) {
29        this.__strings__.push(str);
30    }
;
31 img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif    StringBuffer.prototype.toString  =  function () {
32        return this.__strings__.join('');
33    }
;
34     
35      // 调用StringBuffer类,实现拼接字符串
36      // 每次完成字符串连接都会执行步骤2步
37      // 实际上,这段代码在幕后执行的步骤如下:
38 img_405b18b4b6584ae338e0f6ecaf736533.gifimg_1c53668bcee393edac0d7b3b3daff1ae.gif      /*
39        1.创建存储结果的字符串
40        2.把每个字符串复制到结果中的合适位置
41    */

42      var  buffer  =  new  StringBuffer();
43     buffer.append( ' hello  ' );
44     buffer.append( ' world ' );
45      var  result  =  buffer.toString();
46     
47      // 用StringBuffer类比使用+=节省50%~66%的时间
48 // -->
49 < / script>
50 < / html>
51
目录
相关文章
|
2月前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
245 100
|
2月前
|
开发者 Python
Python中的f-string:高效字符串格式化的利器
Python中的f-string:高效字符串格式化的利器
333 99
|
2月前
|
Python
Python中的f-string:更优雅的字符串格式化
Python中的f-string:更优雅的字符串格式化
|
2月前
|
开发者 Python
Python f-string:高效字符串格式化的艺术
Python f-string:高效字符串格式化的艺术
|
3月前
|
Python
Python中的f-string:更简洁的字符串格式化
Python中的f-string:更简洁的字符串格式化
241 92
|
1月前
|
编解码 Java 开发者
Java String类的关键方法总结
以上总结了Java `String` 类最常见和重要功能性方法。每种操作都对应着日常编程任务,并且理解每种操作如何影响及处理 `Strings` 对于任何使用 Java 的开发者来说都至关重要。
195 5
|
4月前
|
自然语言处理 Java Apache
在Java中将String字符串转换为算术表达式并计算
具体的实现逻辑需要填写在 `Tokenizer`和 `ExpressionParser`类中,这里只提供了大概的框架。在实际实现时 `Tokenizer`应该提供分词逻辑,把输入的字符串转换成Token序列。而 `ExpressionParser`应当通过递归下降的方式依次解析
283 14
|
5月前
|
存储 编译器 C语言
关于string的‘\0‘与string,vector构造特点,反迭代器与迭代器类等的讨论
你真的了解string的'\0'么?你知道创建一个string a("abcddddddddddddddddddddddddd", 16);这样的string对象要创建多少个对象么?你知道string与vector进行扩容时进行了怎么的操作么?你知道怎么求Vector 最大 最小值 索引 位置么?
106 0