safe-json-parse

Benchmark created by sorensen on


Setup

function safe1(x) {
     if (typeof x !== 'string' || (x[0] !== '{' && x[0] !== '[' )) return x
     try { return JSON.parse(x) }
     catch(e) { return x }
    }
    function safe2(x) {
     try { return JSON.parse(x) }
     catch(e) { return x }
    }

Test runner

Ready to run.

Testing in
TestOps/sec
safe-1
safe1('{"a": "3"}')
safe1('[1,2,3]')
safe1('error')
ready
safe-2
safe2('{"a": "3"}')
safe2('[1,2,3]')
safe2('error')
ready

Revisions

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

  • Revision 1: published by sorensen on