测试JavaScript字符串处理性能
来源:程序员人生 发布时间:2014-05-24 13:41:05 阅读次数:3802次
主要是测试了数组 push()方法与直接相加这两种在JS引擎上性能的实际处理情况。程序代码简单明了,没什么好解释的, 注:循环次数必须足够大。
var oldtime = new Date().getTime();
var str = [];
for(i=0;i<100000;i++)
{
str.push('abcabcabcabcabcabcabcabcabcabcabcabc');
}
var newtime = new Date().getTime();
var joinTime = (newtime - oldtime);
var oldtime = new Date().getTime();
var str='';
for(i=0;i<100000;i++)
{
str += 'abcabcabcabcabcabcabcabcabcabcabcabc';
}
var newtime = new Date().getTime();
var concatTime = (newtime - oldtime);
alert("join / concat: " + (joinTime * 1.0 / concatTime));