blob: 5100ab2b80f99624b0da3d3b2b08831b1dce3d8a [file] [log] [blame]
import vuvuzela from 'vuvuzela';
function slowJsonParse(str) {
try {
return JSON.parse(str);
} catch (e) {
/* istanbul ignore next */
return vuvuzela.parse(str);
}
}
function safeJsonParse(str) {
// try/catch is deoptimized in V8, leading to slower
// times than we'd like to have. Most documents are _not_
// huge, and do not require a slower code path just to parse them.
// We can be pretty sure that a document under 50000 characters
// will not be so deeply nested as to throw a stack overflow error
// (depends on the engine and available memory, though, so this is
// just a hunch). 50000 was chosen based on the average length
// of this string in our test suite, to try to find a number that covers
// most of our test cases (26 over this size, 26378 under it).
if (str.length < 50000) {
return JSON.parse(str);
}
return slowJsonParse(str);
}
export default safeJsonParse;