欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

测试JavaScript字符串处理性能的代码_javascript技巧

程序员文章站 2022-04-04 22:38:03
...
复制代码 代码如下:

var oldtime = new Date().getTime();
var str = [];
for(i=0;i{
str.push('abcabcabcabcabcabcabcabcabcabcabcabc');
}
var newtime = new Date().getTime();
var joinTime = (newtime - oldtime);

var oldtime = new Date().getTime();
var str='';
for(i=0;i{
str += 'abcabcabcabcabcabcabcabcabcabcabcabc';
}
var newtime = new Date().getTime();
var concatTime = (newtime - oldtime);

alert("join / concat: " + (joinTime * 1.0 / concatTime));

注:循环次数必须足够大。