[Performance] Issue with `JSON.parse`
jarrodek opened this issue · comments
There's a place in the code somewhere where the parser does something like this:
function r(e){
try{
var t=JSON.parse(e);
return t.$schema
} catch(n){
return!1
}
(this is from compiled source)
I made a jsPerf to test this approach and according to test results it's much, much faster to check first letter of the string for only two possible characters for valid JSON: {
or [
.
JSON can be only an object or an array so it make sense to test it first before invoking JSON parser.
See jsPerf here: https://jsperf.com/json-parse-test-vs-json-format-test
According to this you can save ~95% of the time while processing schemas.
Note that raml-js-parser-2 has been deprecated, the new official parser is webapi-parser. Feel free to attempt to reproduce this issue with webapi-parser and report any issue you may have on that repository.
I am using AMF parser now.