俺们以使用AJAX来开劳务器端和客户端交互的时光,一般的做法是为服务器端返回一段落JSON字符串,然后以客户端将它们解析成JavaScript对象。解析时用到的主意一般是eval或者new
function,而当前IE8和Firefox3.1以置了原生的JSON对象(据说会发生得的性能提升)。那咱们于事实上运用的早晚如何从马上三种植艺术(因为性问题,不考虑用javascript实现的分析)里面来选择吧?面对重重的浏览器,哪种艺术的性能是极致好之啊?

一样、测试方法

1、首先指定测试次数与JSON字符串

   1: 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

    1: var beginTime = new Date();

    2: for ( i = 0; i < count; i++ ) {

    3: o = eval( “(” + jsonString + “)” );

    4: }

    5: Console.output( “eval:” + ( new Date() – beginTime ) );

  • new Function

    1: var beginTime = new Date();

    2: for ( i = 0; i < count; i++ ) {

    3: o = new Function( “return ” + jsonString )();

    4: }

    5: Console.output( “new Function:” + ( new Date() – beginTime ) );

  • native

    1: if ( typeof JSON !== “undefined” ) {

    2: var beginTime = new Date();

    3: for ( i = 0; i < count; i++ ) {

    4: o = JSON.parse( jsonString ); }

    5: Console.output( “native:” + ( new Date() – beginTime ) );

    6: } else {

    7: Console.output( “native:not support!” );

    8: }

亚、测试目标

择时主流的浏览器(不考虑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

*数值越小更好

style=”font-size: xx-small”>*每当现阶段排着绿色背景的意味性能最好,红色性能最好差

1、Firefox2、3全套垫底,IE6的性质优于IE7(可能和机器不雷同有关),Chrome和Safari4的特性远远超越其它浏览器。

2、不同之浏览器下eval和new
Function的性质不平等,总的来说eval更好,但Firefox下new
Function的习性是eval的同等倍,为了重新好之匹配各个浏览器,我们将针对JSON的剖析单独封装成一个目标来处理:

  • wrapper

    1: var __json = null;

    2: if ( typeof JSON !== “undefined” ) {

    3: __json = JSON;

    4: }

    5: var browser = Browser;

    6: var JSON = {

    7: parse: function( text ) {

    8: if ( __json !== null ) {

    9: return __json.parse( text );

    10: }

    11: if ( browser.gecko ) {

    12: return new Function( “return ” + text )();

    13: }

    14: return eval( “(” + text + “)” )

    15: }

    16: };

    17: var beginTime = new Date();

    18: for ( i = 0; i < count; i++ ) {

    19: o = JSON.parse( jsonString ); }

    20: Console.output( “wrapper:” + ( new Date() – beginTime ) );

入Wrapper后的结果:

图片 2

出于涉及到调用对象的开,封装后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生测试了瞬间,结果如下:

图片 3 

及时表明Firefox本身的习性并无是形象我们原先测试的那么没有,在错过丢插件后性能还是生科学。但是没Firebug一近乎的插件支持,Firefox对我们的引力为大大降低了。

 

  • 2009.03.31:循环中老是用新的json字符串

根据Oliver的讲述,他猜测是出于Safari4和Chrome缓存了eval的结果用导致她的测试成绩“虚”高,测试结果印证了他的推理:

图片 4

起这结果被我们得看来,Opera的性最好好,Ie8其次。

重点修改的代码:

   1: //eval

   2: var beginTime = new Date();

   3: for ( i = 0; i < count; i++ ) {

   4:     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}' + ")");

   5: }

   6: Console.output( "eval:" + ( new Date() - beginTime ) );

   7: //new Function

   8: beginTime = new Date();

   9: for ( i = 0; i < count; i++ ) {

  10:     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}')();

  11: }

  12: Console.output( "new Function:" + ( new Date() - beginTime ) );

  13: //native

  14: if ( typeof JSON !== "undefined" ) {

  15:     beginTime = new Date();

  16:     for ( i = 0; i < count; i++ ) {

  17:         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}');

  18:     }

  19:     Console.output( "native:" + ( new Date() - beginTime ) );

  20: } else {

  21:     Console.output( "native:not support!" );

  22: }

  23: //wrapper

  24: var __json = null;

  25: if ( typeof JSON !== "undefined" ) {

  26:     __json = JSON;

  27: }

  28: var browser = Browser;

  29: var JSON = {

  30:     parse: function( text ) {

  31:         if ( __json !== null ) {

  32:             return __json.parse( text );

  33:         }

  34:         if ( browser.gecko ) {

  35:             return new Function( "return " + text )();

  36:         }

  37:         return eval( "(" + text + ")" )

  38:     }

  39: };          

  40: beginTime = new Date();

  41: for ( i = 0; i < count; i++ ) {

  42:     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}');

  43: }

  44: Console.output( "wrapper:" + ( new Date() - beginTime ) );

 

促:全部代码

 

图片 5图片 6Code
<!DOCTYPE HTML PUBLIC “-//W3C//DTD HTML 4.01//EN”
“http://www.w3.org/TR/html4/strict.dtd"&gt;
<html>
    <head>
        <meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
        <title>Parse
JsonString</title>
    </head>
    <body>
        <div id=”consoleRegion”></div>
        <script type=”text/javascript”>
            //yui
           
var Browser = function() {
                var o = {
                    ie: 0,
                    opera: 0,
                    gecko: 0,
                    webkit: 0
                };
                var ua = navigator.userAgent,
m;
                if ( ( /KHTML/ ).test( ua ) ) {
                    o.webkit = 1;
                }
                // Modern WebKit
browsers are at least X-Grade
                m
= ua.match(/AppleWebKit\/([^\s]*)/);
               
if (m&&m[1]) {
                    o.webkit=parseFloat(m[1]);
                }

                if (!o.webkit) {
// not
webkit
                   
// @todo check
Opera/8.01 (J2ME/MIDP; Opera Mini/2.0.4509/1316; fi; U; ssr)
                   
m=ua.match(/Opera[\s\/]([^\s]*)/);
                   
if (m&&m[1]) {
                        o.opera=parseFloat(m[1]);
                    } else { // not opera or
webkit
                       
m=ua.match(/MSIE\s([^;]*)/);
                        if (m&&m[1]) {
                            o.ie=parseFloat(m[1]);
                        } else { // not opera, webkit, or
ie
                           
m=ua.match(/Gecko\/([^\s]*)/);
                           
if (m) {
                                o.gecko=1; // Gecko detected, look
for revision
                               
m=ua.match(/rv:([^\s\)]*)/);
                                if (m&&m[1]) {
                                    o.gecko=parseFloat(m[1]);
                                }
                            }
                        }
                    }
                }
                return o;
            }();

            var Console = {
                consoleRegion: null,

                getRegion: function() {
                    if ( this.consoleRegion
=== null ) {

                        this.consoleRegion

document.getElementById( “consoleRegion” );
                    }
                    return this.consoleRegion;
                },

                output: function( text ) {

                    this.getRegion().innerHTML
+= “<br/>” + text;

                }
            };
            //test code
           
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}’;
            //eval
           
var beginTime
= new Date();
            for ( i = 0; i < count; i++ ) {
                o = eval( “(” + jsonString
+ “)” );
            }
            Console.output( “eval:” + ( new Date() – beginTime ) );
            //new
Function
            beginTime
= new Date();
            for ( i = 0; i < count; i++ ) {
                o = new Function(
“return ” + jsonString )();
            }
            Console.output( “new
Function:” + ( new Date() – beginTime ) );
            //native
           
if ( typeof JSON !== “undefined” ) {
                beginTime = new Date();
                for ( i = 0; i < count; i++ ) {
                    o = JSON.parse(
jsonString );

                }
                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 < count; i++ ) {
                o = JSON.parse(
jsonString );

            }
            Console.output( “wrapper:” + ( new Date() – beginTime ) );
            //alert(
o.value.items[0].z );
       
</script>
    </body>
</html>

相关文章

网站地图xml地图