check json validity : try/catch vs regex (v2)

Revision 2 of this benchmark created on


Setup

var isJsonRegex = function(str) {
      if (str == '') return false;
      str = str.replace(/\\./g, '@').replace(/"[^"\\\n\r]*"/g, '');
      return (/^[,:{}\[\]0-9.\-+Eaeflnr-u \n\r\t]*$/).test(str);
    }
    
    var isJsonTryCatch = function() {
      try {
        o = JSON.parse(text);
        return true;
      } catch (e) {}
      return false;
    }

Test runner

Ready to run.

Testing in
TestOps/sec
Regex validation
// async test
var text_valid = '{"a":"obja","b":[0,1,2],"c":{"d":"some object"}}';
var text_invalid = '{"a":"obja""b":[0,1,2],"c":{"d":"some object"}}';
var valid = isJsonRegex(text_valid);
var invalid = isJsonRegex(text_invalid);
ready
Try/Catch validation
// async test
var text_valid = '{"a":"obja","b":[0,1,2],"c":{"d":"some object"}}';
var text_invalid = '{"a":"obja""b":[0,1,2],"c":{"d":"some object"}}';
var valid = isJsonTryCatch(text_valid);
var invalid = isJsonTryCatch(text_invalid);
ready

Revisions

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