JSON Parsing

JavaScript performance comparison

Revision 12 of this test case created by Tom

Preparation code

<script>
  var myjson = '{"mylist": [4, 8, 15, 16, 23, 42], "myobject": {"hello": "world"}, "myint": 23, "mystrlist": ["foo", "bar"]}',
      cachedEval = eval('(' + myjson + ')'),
      cachedFunction = (new Function('return ' + myjson)); // *deprecated
  
  function tuenti_decode(string, secure) {
   if (typeof string != "string" || !string.length) {
    return null;
   }
   if (secure && !/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/.test(string.replace(/\\./g, "@").replace(/"[^"\\\n\r]*"/g, ""))) {
    return null;
   }
   return eval("(" + string + ")");
  }
  
  function tuenti_secure(string) {
   var regexp1 = /^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/,
       regexp2 = /\\./g,
       regexp3 = /"[^"\\\n\r]*"/g;
   if (!string || typeof string !== "string") {
    return null;
   }
   if (regexp1.test(string.replace(regexp2, "@").replace(regexp3, ""))) {
    return null;
   }
   return eval("(" + string + ")");
  }
</script>
    

Test runner

Warning! For accurate results, please disable Firebug before running the tests. (Why?)

Java applet disabled.

Testing in CCBot 2.0.0 / Other 0.0.0
Test Ops/sec
Evil
eval('(' + myjson + ')');
pending…
JSON.parse
JSON.parse(myjson);
pending…
new Function
(new Function('return ' + myjson))();
pending…
Tuenti implementation
tuenti_decode(myjson);
pending…
Tuenti secure
tuenti_decode(myjson, true);
pending…
Tuenti secure optimized
tuenti_secure(myjson);
pending…

Compare results of other browsers

Revisions

You can edit these tests or add even more tests to this page by appending /edit to the URL.

0 Comments