当前位置: 首页 > 图文教程 > 网络编程 > Javascript > 效率高的Javscript字符串替换函数的benchmark

Javascript
form中限制文本字节数js代码
use jscript with List Proxy Server Information
use jscript List Installed Software
List Installed Software Features
List Information About the Binary Files Used by an Application
List the Codec Files on a Computer
List the UTC Time on a Computer
List Installed Hot Fixes
excel操作之Add Data to a Spreadsheet Cell
Add Formatted Data to a Spreadsheet
Apply an AutoFormat to an Excel Spreadsheet
JavaScript语法着色引擎(demo及打包文件下载)
类之Prototype.js学习
一款JavaScript压缩工具:X2JSCompactor
iis6+javascript Add an Extension File
jscript之Open an Excel Spreadsheet
jscript之Read an Excel Spreadsheet
jscript之List Excel Color Values
去除图像或链接黑眼圈的两种方法总结
Add a Formatted Table to a Word Document

Javascript 中的 效率高的Javscript字符串替换函数的benchmark


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-12   浏览: 227 ::
收藏到网摘: n/a

这是经常使用的HTML特殊字符替换函数,即将 &、<、>、" 等函数替换成 &、<、>、"。通常的作法都是连续使用数个 replace 函数,而Clear仅使用一个 replace 就完成了替换。 可以任意转载,但转载时必须标明原作者charlee、原始链接http://tech.idv2.com/2006/11/23/javascript-strreplace-benchmark/以及本声明

我对这两个函数作了benchmark,发现新函数的效率约为原函数的1.4倍。虽然没有预想中那么高效率,但当要替换的字符串更多时,效率应该会提高吧。

测试方法为,对一个2048字节长的字符串分别执行两个函数100次,取其执行时间。下面为10次测试的结果。toTXT_1 为通常的作法,toTXT_2 为Clear的方法。

toTXT_1(ms) toTXT_2(ms) toTXT_1/toTXT_2
1609 1204 75%
2250 1547 69%
1672 1313 79%
1813 1250 69%
1844 1219 66%
2063 1421 69%
2031 1172 58%
1797 1360 76%
2015 1281 64%
1860 1500 81%

测试程序在这里http://img.ruanchen.com/nline/javascript-benchmark/benchmark.htm