您好,欢迎访问一九零五行业门户网

JavaScript 解析Json字符串的性能比较分析代码_json

解析时用到的方法一般是eval或者new function,而目前ie8和firefox3.1又内置了原生的json对象(据说会有一定的性能提升)。那我们在实际使用的时候怎样从这三种方法(因为性能问题,不考虑用javascript实现的解析)里面来选择呢?面对众多的浏览器,哪种方式的性能是最好的呢?
一、测试方法
1、首先指定测试次数及json字符串
复制代码 代码如下:
var count = 10000, o = null, i = 0, jsonstring = '{value:{items: [{x:1,y:2,z:3}, {x:1,y:2,z:3}, {x:1,y:2,z:3}, {x:1,y:2,z:3}, {x:1,y:2,z:3}]},error:null}';
2、循环解析并记录时间
eval
复制代码 代码如下:
var begintime = new date();
for ( i = 0; i o = eval( ( + jsonstring + ) );
}
console.output( eval: + ( new date() - begintime ) );
new function
复制代码 代码如下:
var begintime = new date();
for ( i = 0; i o = new function( return + jsonstring )();
}
console.output( new function: + ( new date() - begintime ) );
native
复制代码 代码如下:
if ( typeof json !== undefined ) {
var begintime = new date();
for ( i = 0; i o = json.parse( jsonstring ); }
console.output( native: + ( new date() - begintime ) );
} else {
console.output( native:not support! );
}
二、测试对象
选择目前主流的浏览器(不考虑maxthon一类的外壳),包括ie6、7、8,firefox2、3、3.1,chrome,opera及safari3、4。
三、测试环境
t9300 cpu + 4g ram + windows2003,其中ie8使用的是vista的环境,ie7在另外一台工作机(2g cpu + 2g ram + windows2003),考虑到主要是测试浏览器客户端的性能,结果的误差应该能够接受。
四、测试结果
*数值越小越好
*在当前列中绿色背景的表示性能最好,红色性能最差
1、firefox2、3全部垫底,ie6的性能优于ie7(可能和机器不一致有关),chrome和safari4的性能远远超出其它浏览器。
2、不同的浏览器下eval和new function的性能不一致,总的来说eval更好,但firefox下new function的性能是eval的一倍,为了更好的兼容各个浏览器,我们把对json的解析单独封装成一个对象来处理:
wrapper
复制代码 代码如下:
var __json = null;
if ( typeof json !== undefined ) {
__json = json;
}
var browser = browser;
var json = {
parse: function( text ) {
if ( __json !== null ) {
return __json.parse( text );
}
if ( browser.gecko ) {
return new function( return + text )();
}
return eval( ( + text + ) )
}
};
var begintime = new date();
for ( i = 0; i o = json.parse( jsonstring ); }
console.output( wrapper: + ( new date() - begintime ) );
加入wrapper后的结果:
由于涉及到调用对象的开销,封装后json对象会比单独调用更慢,但它能保证在各个浏览器下使用最适合的方法。
五、结论
解析json字符串时,不同的浏览器选择不同的方法:
ie6、7使用eval
ie8使用原生的json对象
firefox2、3使用new function
safari4使用eval
其它浏览器下eval和new function的性能基本一致
如果有不同意见欢迎拍砖:)
update:
2009.03.23:屏蔽所有firefox的add-ons再进行测试
由于known在firefox下运行代码得到了完全不一致的结果,怀疑是firefox的插件导致,于是禁掉所有插件后(后来表明几乎由firebug导致),重新在firefox2、3下测试了一下,结果如下:
这表明firefox本身的性能并不是象我们先前测试的那样低,在去掉插件后性能还是很不错。但是没有firebug一类的插件支持,firefox对我们的吸引力也大大降低了。
2009.03.31:循环中每次使用新的json字符串
根据oliver的描述,他猜测是由于safari4和chrome缓存了eval的结果从而导致它们的测试成绩“虚”高,测试结果证明了他的推测:
从这个结果中我们可以看到,opera的性能最好,ie8其次。
主要修改的代码:
复制代码 代码如下:
//eval 2: var begintime = new date();
for ( i = 0; i o = eval(( + '{value:{items: [{x:' + i + ',y:2,z:3}, {x:1,y:2,z:3}, {x:1,y:2,z:3}, {x:1,y:2,z:3}, {x:1,y:2,z:3}]},error:null}' + ));
}
console.output( eval: + ( new date() - begintime ) );
//new function
begintime = new date();
for ( i = 0; i o = new function(return + '{value:{items: [{x:' + i + ',y:2,z:3}, {x:1,y:2,z:3}, {x:1,y:2,z:3}, {x:1,y:2,z:3}, {x:1,y:2,z:3}]},error:null}')();
}
console.output( new function: + ( new date() - begintime ) );
//native
if ( typeof json !== undefined ) {
begintime = new date();
for ( i = 0; i o = json.parse('{value:{items: [{x:' + i + ',y:2,z:3}, {x:1,y:2,z:3}, {x:1,y:2,z:3}, {x:1,y:2,z:3}, {x:1,y:2,z:3}]},error:null}');
}
console.output( native: + ( new date() - begintime ) );
} else {
console.output( native:not support! );
}
//wrapper
var __json = null;
if ( typeof json !== undefined ) {
__json = json;
}
var browser = browser;
var json = {
parse: function( text ) {
if ( __json !== null ) {
return __json.parse( text );
}
if ( browser.gecko ) {
return new function( return + text )();
}
return eval( ( + text + ) )
}
};
begintime = new date();
for ( i = 0; i o = json.parse('{value:{items: [{x:' + i + ',y:2,z:3}, {x:1,y:2,z:3}, {x:1,y:2,z:3}, {x:1,y:2,z:3}, {x:1,y:2,z:3}]},error:null}');
}
console.output( wrapper: + ( new date() - begintime ) );
附:全部代码
复制代码 代码如下:
parse jsonstring
其它类似信息

推荐信息