Parsing optimization or lazy loading
I have a serious performance issue when parsing.
To demonstrate how it works, I wrote the following code to compare the performance of native JSON.parse and Json { ignoreUnknownKeys = true }.decodeFromString

After that, I perform a time measurement via chrome profile and get this result
Json { ignoreUnknownKeys = true }.decodeFromString 1.92s

JSON.parse 23ms

Is it possible to somehow speed up the work? Use partial JSON.parse in js implementation? For example, create a Kotlin DTO class and populate it like this?
@Serializable
class Foo {
val bas: String
}
let parseData = JSON.parse ('{"bas": "hello"}')
Object.assing (new Foo (), parseData)
is it possible to somehow speed it up?
Some ways to get better speed in Kotlin/JS serialization:
- Don't create
Jsoninstance every time. Cache it withval json = JSON { ... my options ... } - Use
JSON.parsefollowed byjson.decodeFromDynamic(see https://kotlin.github.io/kotlinx.serialization/kotlinx-serialization-json/kotlinx-serialization-json/kotlinx.serialization.json/decode-from-dynamic.html)
(Measure your results, the benefit, if any, depends on your specific data)
The option you proposed takes even longer.


And the JSON object is created once.
But what is the JSON object? Can you provide a self-contained example that we can use to repeat your measurements?
More json https://kotlinlang.org/api/latest/jvm/stdlib/kotlin.js/-j-s-o-n/
Unfortunately, I cannot provide you with this particular JSON as it is part of the NDA.
But I can show pieces that could slow down the parsing

This is still relevant, deserializing with kotlinx.serialization is 20x slower than using JSON.parse This is an old example with a 200kb json: https://github.com/SerVB/kx-serialization-js-benchmark but the timings are the same with the latest libraries
@sandwwraith any progress on this? are there any short term plans for this? Thanks
Is there any estimation for this to be fixed/improved?
Sadly, there are no immediate plans for this. I'd also like to point out that most Kotlin objects contain additional information, so we need to create them during deserialization — I suppose that's where the main difference is coming from.
I suggest trying decodeFromDynamic over the result of JSON.parse, maybe this will be faster in your case. Just remember to check the results, because they may slightly differ.
@ApoloApps Indeed, it was not adopted for wasm. Can you open a new issue please?