(#4861) - faster json parse
diff --git a/src/deps/safeJsonParse.js b/src/deps/safeJsonParse.js index ee0de96..5100ab2 100644 --- a/src/deps/safeJsonParse.js +++ b/src/deps/safeJsonParse.js
@@ -1,6 +1,6 @@ import vuvuzela from 'vuvuzela'; -function safeJsonParse(str) { +function slowJsonParse(str) { try { return JSON.parse(str); } catch (e) { @@ -9,4 +9,20 @@ } } +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; \ No newline at end of file