1: <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2: <html>
3: <head>
4: <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
5: <title>Parse JsonString</title>
6: </head>
7: <body>
8: <div id="consoleRegion"></div>
9: <script type="text/javascript">
10: //yui
11: var Browser = function() {
12: var o = {
13: ie: 0,
14: opera: 0,
15: gecko: 0,
16: webkit: 0
17: };
18: var ua = navigator.userAgent, m;
19: if ( ( /KHTML/ ).test( ua ) ) {
20: o.webkit = 1;
21: }
22: // Modern WebKit browsers are at least X-Grade
23: m = ua.match(/AppleWebKit\/([^\s]*)/);
24: if (m&&m[1]) {
25: o.webkit=parseFloat(m[1]);
26: }
27:
28: if (!o.webkit) { // not webkit
29: // @todo check Opera/8.01 (J2ME/MIDP; Opera Mini/2.0.4509/1316; fi; U; ssr)
30: m=ua.match(/Opera[\s\/]([^\s]*)/);
31: if (m&&m[1]) {
32: o.opera=parseFloat(m[1]);
33: } else { // not opera or webkit
34: m=ua.match(/MSIE\s([^;]*)/);
35: if (m&&m[1]) {
36: o.ie=parseFloat(m[1]);
37: } else { // not opera, webkit, or ie
38: m=ua.match(/Gecko\/([^\s]*)/);
39: if (m) {
40: o.gecko=1; // Gecko detected, look for revision
41: m=ua.match(/rv:([^\s\)]*)/);
42: if (m&&m[1]) {
43: o.gecko=parseFloat(m[1]);
44: }
45: }
46: }
47: }
48: }
49: return o;
50: }();
51:
52: var Console = {
53: consoleRegion: null,
54:
55: getRegion: function() {
56: if ( this.consoleRegion === null ) {
57: this.consoleRegion = document.getElementById( "consoleRegion" );
58: }
59: return this.consoleRegion;
60: },
61:
62: output: function( text ) { this.getRegion().innerHTML += "<br/>" + text; }
63: };
64: //test code
65: 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}';
66: //eval
67: var beginTime = new Date();
68: for ( i = 0; i < count; i++ ) {
69: o = eval( "(" + jsonString + ")" );
70: }
71: Console.output( "eval:" + ( new Date() - beginTime ) );
72: //new Function
73: beginTime = new Date();
74: for ( i = 0; i < count; i++ ) {
75: o = new Function( "return " + jsonString )();
76: }
77: Console.output( "new Function:" + ( new Date() - beginTime ) );
78: //native
79: if ( typeof JSON !== "undefined" ) {
80: beginTime = new Date();
81: for ( i = 0; i < count; i++ ) {
82: o = JSON.parse( jsonString ); }
83: Console.output( "native:" + ( new Date() - beginTime ) );
84: } else {
85: Console.output( "native:not support!" );
86: }
87: //wrapper
88: var __json = null;
89: if ( typeof JSON !== "undefined" ) {
90: __json = JSON;
91: }
92: var browser = Browser;
93: var JSON = {
94: parse: function( text ) {
95: if ( __json !== null ) {
96: return __json.parse( text );
97: }
98: if ( browser.gecko ) {
99: return new Function( "return " + text )();
100: }
101: return eval( "(" + text + ")" )
102: }
103: };
104: beginTime = new Date();
105: for ( i = 0; i < count; i++ ) {
106: o = JSON.parse( jsonString ); }
107: Console.output( "wrapper:" + ( new Date() - beginTime ) );
108: //alert( o.value.items[0].z );
109: </script>
110: </body>
111: </html>