javascript 字符串连接的性能问题(多浏览器)
2008-11-18 00:00
931 查看
书中附带的测试代码如下
在 Firefox/3.0.3中执行的结果如下:
Concatenation with plus: 5 milliseconds
Concatenation with StringBuffer: 10 milliseconds
在IE6中执行结果如下:
Concatenation with plus: 234 milliseconds
Concatenation with StringBuffer: 62 milliseconds
1.两种方式性能差别很大
2.看来IE6字符串连接处理能力比FF3很差呀
3.IE6和FF3两种方式结果相反,看来以后写连接优化还有注意浏览器呀
<html> <head> <title>Example</title> </head> <body> <p><strong>Note:</strong> The latest versions of Firefox seem to have fixed the string concatenation problem. If you are using Firefox 1.0 or later, the string buffer may actually take longer than normal string concatenation.</p> <script type="text/javascript"> function StringBuffer() { this.__strings__ = new Array; } StringBuffer.prototype.append = function (str) { this.__strings__.push(str); }; StringBuffer.prototype.toString = function () { return this.__strings__.join(""); }; var d1 = new Date(); var str = ""; for (var i=0; i < 10000; i++) { str += "text"; } var d2 = new Date(); document.write("Concatenation with plus: " + (d2.getTime() - d1.getTime()) + " milliseconds"); var buffer = new StringBuffer(); d1 = new Date(); for (var i=0; i < 10000; i++) { buffer.append("text"); } var result = buffer.toString(); d2 = new Date(); document.write("<br />Concatenation with StringBuffer: " + (d2.getTime() - d1.getTime()) + " milliseconds"); </script> </body> </html>
在 Firefox/3.0.3中执行的结果如下:
Concatenation with plus: 5 milliseconds
Concatenation with StringBuffer: 10 milliseconds
在IE6中执行结果如下:
Concatenation with plus: 234 milliseconds
Concatenation with StringBuffer: 62 milliseconds
1.两种方式性能差别很大
2.看来IE6字符串连接处理能力比FF3很差呀
3.IE6和FF3两种方式结果相反,看来以后写连接优化还有注意浏览器呀
相关文章推荐
- javascript 字符串连接的性能问题(多浏览器)
- javascript 字符串连接的性能问题(多浏览器)
- 你必须知道的JavaScript 中字符串连接的性能的一些问题
- javascript字符串连接性能的问题
- javascript字符串连接性能的问题
- 你必须知道的JavaScript 中字符串连接的性能的一些问题
- 你不得不知道的关于JavaScript 中字符串连接的性能问题
- 你不得不知道的关于JavaScript 中字符串连接的性能问题
- JavaScript 字符串连接性能优化
- Java字符串连接操作的性能问题
- 比较javascript两种字符串连接的性能
- javascript中字符串连接的性能
- Javascript 字符串浏览器兼容问题
- ECMAScript字符串连接性能问题
- 数组方法解决JS字符串连接性能问题有争议
- 温故知新――JavaScript中的字符串连接问题最全总结(推荐)
- VBS中的字符串连接的性能问题
- 小技巧:用Javascript数组处理多个字符串的连接问题
- JavaScript中字符串连接性能分析
- 关于javascript字符串连接性能