Using Utf8JsonReader.ValueTextEquals() cause a drop of performance of about 400% compared to bytes comparison with the signature algorithm scenario.
Using Encoding.UTF8.GetBytes(Utf8JsonReader.GetString()) cause a drop of performance of about 80% and an useless allocation of the string object.
Using Utf8JsonReader.ValueSpan is fastest, but the value is not unescaped.
If the header is represented as {"alg":"HS256"}it is fine, but if it is represented as {"alg":"\u0048\u0053\u0032\u0035\u0036"}, which is not very likely but possible, it will not be correctly interpreted.
An option would be to combine Utf8JsonReader.ValueSpan for most common scenario, and as fallback use Encoding.UTF8.GetBytes(Utf8JsonReader.GetString()).
Another option would be to have a API like Utf8JsonReader.GetUtf8String() for extracting the UTF8 unescaped string as its byted representation, similar to the UTF8JsonReader.GetBytesFromBase64() API, but it may still cause some memory allocation.
CC @ahsonkhan
Using
Utf8JsonReader.ValueTextEquals()
cause a drop of performance of about 400% compared to bytes comparison with the signature algorithm scenario. UsingEncoding.UTF8.GetBytes(Utf8JsonReader.GetString())
cause a drop of performance of about 80% and an useless allocation of thestring
object. UsingUtf8JsonReader.ValueSpan
is fastest, but the value is not unescaped. If the header is represented as{"alg":"HS256"}
it is fine, but if it is represented as{"alg":"\u0048\u0053\u0032\u0035\u0036"}
, which is not very likely but possible, it will not be correctly interpreted.An option would be to combine
Utf8JsonReader.ValueSpan
for most common scenario, and as fallback useEncoding.UTF8.GetBytes(Utf8JsonReader.GetString())
. Another option would be to have a API likeUtf8JsonReader.GetUtf8String()
for extracting the UTF8 unescaped string as its byted representation, similar to theUTF8JsonReader.GetBytesFromBase64()
API, but it may still cause some memory allocation. CC @ahsonkhan