我有段JavaScript代码如下:var DATA = [
["00002","9900","","","2009-11-23 17:51:52","0001","0002","+1.5","","","","","","",""],
["00003","9900","","","2009-11-23 17:52:15","0001","0002","-1","","","","","","","0"],
["00004","9900","","","2009-11-23 17:53:02","0001","0002","-1","","","","","","","0"],
["00005","9900","","","2009-11-23 17:52:52","0001","0002","-1","","","","","","","0"],
["00006","9900","","","2009-11-23 17:52:52","0001","0002","-1","","","","","","","0"],
["00007","9900","","","2009-11-23 17:52:52","0001","0002","-1","","","","","","","0"],
["00008","9900","","","2009-11-23 17:52:52","0001","0002","-1","","","","","","","0"],
//...后面还有很多,一共有5000多条
];
var strB = '';
function test(){
for(var i=0,len=DATA.length;i<len;i++) {
strB += '\r\n';
}
}
var t1=Date.parse(new Date()) + new Date().getMilliseconds();
test();
var t2=Date.parse(new Date()) + new Date().getMilliseconds();
alert(t2-t1);这样一段函数在IE下的执行下要几秒钟,而FF、Chrome下只要几毫秒
如果我把上面那个数组给注释掉,再在IE下执行,也只需要几毫秒难道前面的大数组会影响后面的函数的执行??我函数里根本就没有用到这个大数组啊请高手告诉一下原因,以及在IE下如何改进
["00002","9900","","","2009-11-23 17:51:52","0001","0002","+1.5","","","","","","",""],
["00003","9900","","","2009-11-23 17:52:15","0001","0002","-1","","","","","","","0"],
["00004","9900","","","2009-11-23 17:53:02","0001","0002","-1","","","","","","","0"],
["00005","9900","","","2009-11-23 17:52:52","0001","0002","-1","","","","","","","0"],
["00006","9900","","","2009-11-23 17:52:52","0001","0002","-1","","","","","","","0"],
["00007","9900","","","2009-11-23 17:52:52","0001","0002","-1","","","","","","","0"],
["00008","9900","","","2009-11-23 17:52:52","0001","0002","-1","","","","","","","0"],
//...后面还有很多,一共有5000多条
];
var strB = '';
function test(){
for(var i=0,len=DATA.length;i<len;i++) {
strB += '\r\n';
}
}
var t1=Date.parse(new Date()) + new Date().getMilliseconds();
test();
var t2=Date.parse(new Date()) + new Date().getMilliseconds();
alert(t2-t1);这样一段函数在IE下的执行下要几秒钟,而FF、Chrome下只要几毫秒
如果我把上面那个数组给注释掉,再在IE下执行,也只需要几毫秒难道前面的大数组会影响后面的函数的执行??我函数里根本就没有用到这个大数组啊请高手告诉一下原因,以及在IE下如何改进
解决方案 »
- jquery datepicker控件的问题,请教各位
- 为什么单页测试通过,放到程序里就不行呢?
- response.write 与浏览器地址栏的问题
- js到底怎么工作的?
- 怎么样把这小段代码写成一个函数?
- 双击问题
- 简单问题,谢谢
- replace(/(\d{6})(?=\d)(\d{8})(?=\d)/g,"$1 $2 ")用来分隔身份证
- 百分求 IE提示页面变慢 的解决方法
- 多页报表在打印时,如何实现每页的第一行都自动附加报表的表头
- |zyciis| 如何在js文件里面创建一个img对像,但他的路径是对应当前js文件的呢?谢谢
- js实现的可以拖动的对话框,但html的声明写成这样就拖不了了!!急!!请教高手!!在线等!!
var strB = [];
function test(){
var len=DATA.length;
var i=0;
for(;i <len;i++) {
strB[strB.length] = '\r\n';
}
}
var t1=Date.parse(new Date()) + new Date().getMilliseconds();
test();
var t2=Date.parse(new Date()) + new Date().getMilliseconds();
alert(t2-t1);
alert(strB.length);//数组长度
strB.join("");//转字符串
修改方案:var strB = new Array();
function test(){
var dataLen = DATA.length;
for(var i = 0,i < dataLen; i++) {
strB.push("\r\n");
}
}
可见 IE 的大字符串拼接存在严重的效率问题!L@_@K
var strB = new Array();
function test(){
for(var i=0,len=DATA.length; i<len; i++) {
strB.push("\r\n");
}
}
for(;i <len;i++) {
//直接用i,不是更省事
strB[i] = '\r\n';
}
}
记得 IE 会把字符串作为字符数组处理,即 CharArray,每次拼接后产生一个新的 String 实例。
var len=DATA.length;
DATA = null;
for(var i=0;i <len;i++) {
strB += '\r\n';
}当然最好还是不要使用字符串拼接的方式