Remove unnecessary error generation when JsUndefined.asOpt is used #1112
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Pull Request Checklist
Purpose
We discovered that in many cases, when JSON is parsed,
asOpt
is used to read an optional key. This results in a performance penalty because an error is generated each time, but it is not used in any way, as described in the documentation forasOpt
.When
asOpt
is used on a missing key, it is called on theJsUndefined
object. This triggers the execution of theasOpt
function inJsReadable
, which performs unnecessary validation and ultimately returnsNone
after constructing a large number of unnecessary strings.An example of this issue can be seen in the following flame graph:
A simple fix would be to always return
None
whenasOpt
is called on theJsUndefined
object.We tested the performance of the function on version
3.0.4
witchScala 2.13.14
andJava 21
with and without the fix, and the results are as follows:Original version
Fixed version
As can easily be seen, the throughput increase is significant, and in our experience, in real-world applications, usually 70% of the time spent in asOpt will be saved in applications that frequently try to access missing keys.
The following code was used to test the throughput: