JSON Parsing (v12)

Revision 12 of this benchmark created by Tom on


Preparation HTML

<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

Ready to run.

Testing in
TestOps/sec
Evil
eval('(' + myjson + ')');
ready
JSON.parse
JSON.parse(myjson);
ready
new Function
(new Function('return ' + myjson))();
ready
Tuenti implementation
tuenti_decode(myjson);
ready
Tuenti secure
tuenti_decode(myjson, true);
ready
Tuenti secure optimized
tuenti_secure(myjson);
ready

Revisions

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