Serializing date on JSON.parse

Benchmark created by Enzo on


Setup

function json_deserialize_helper(key,value) {
      if ( typeof value === 'string' ) {
        var regexp = /^\d\d\d\d-\d\d-\d\dT\d\d:\d\d:\d\d.\d\d\dZ$/.exec(value);
        if ( regexp ) {
          return new Date(value);
        }
      }
      return value;
    }
    function json_deserialize_helper2(key, value) {
        if (typeof value === 'string') {
            var regexp = /^\{(\d*)\}$/.exec(value);
            if (regexp)
                return new Date(Number(regexp[1]));
        }
        return value;
    }
    
    
    var stringify = '{"test":"2013-09-26T03:05:26.563Z"}';
    var stringify2 = '{"test":"{1427048862005}"}';

Test runner

Ready to run.

Testing in
TestOps/sec
non serialized
JSON.parse(stringify);
ready
serialized
JSON.parse(stringify, json_deserialize_helper);
ready
serialized as timestamp
JSON.parse(stringify2, json_deserialize_helper2);
ready

Revisions

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