Looks like JSON: Regex vs indexOf

Benchmark created by mikezter on


Description

I want a simple check for JSON-likely strings, so i just check for curly braces at the beginning and end of string.

Preparation HTML

<script>
  var str = '{"remark":"Done it today","destination":"2727273232","associated_id":19202,"associated_type":"Thingy"}'
</script>

Test runner

Ready to run.

Testing in
TestOps/sec
indexOf
return (str.indexOf('{') === 0 && str.indexOf('}', str.length - 1) === str.length - 1)
ready
Regex
return (/^\{.*\}$/.exec(str) !== null)
ready
charAt
return (str.charAt(0) === '{' && str.charAt(str.length - 1) === '}')
ready
lastIndexOf
return (str.indexOf('{') === 0 && str.lastIndexOf('}') === str.length - 1)
ready
indexOf with tempvar
var len = str.length;
return (str.indexOf('{') === 0 && str.indexOf('}', len - 1) === len - 1)
ready

Revisions

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

  • Revision 1: published by mikezter on
  • Revision 2: published by Kyle A. Matheny on
  • Revision 3: published by mike on