kotlinx.serialization icon indicating copy to clipboard operation
kotlinx.serialization copied to clipboard

Parsing optimization or lazy loading

Open zhimbura opened this issue 4 years ago • 10 comments

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

image

After that, I perform a time measurement via chrome profile and get this result

Json { ignoreUnknownKeys = true }.decodeFromString 1.92s image

JSON.parse 23ms image

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?

zhimbura avatar Apr 23 '21 12:04 zhimbura

Some ways to get better speed in Kotlin/JS serialization:

  1. Don't create Json instance every time. Cache it with val json = JSON { ... my options ... }
  2. Use JSON.parse followed by json.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)

elizarov avatar Apr 23 '21 12:04 elizarov

The option you proposed takes even longer.

image

image

And the JSON object is created once.

zhimbura avatar Apr 23 '21 13:04 zhimbura

But what is the JSON object? Can you provide a self-contained example that we can use to repeat your measurements?

elizarov avatar Apr 23 '21 13:04 elizarov

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

image

zhimbura avatar Apr 23 '21 13:04 zhimbura

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

EmmanueleVilla avatar Jul 29 '25 15:07 EmmanueleVilla

@sandwwraith any progress on this? are there any short term plans for this? Thanks

ApoloApps avatar Sep 03 '25 15:09 ApoloApps

Is there any estimation for this to be fixed/improved?

artemBondar3SS avatar Sep 19 '25 06:09 artemBondar3SS

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.

sandwwraith avatar Sep 19 '25 10:09 sandwwraith

I suggest trying decodeFromDynamic

cant find it for wasmjs

ApoloApps avatar Sep 19 '25 16:09 ApoloApps

@ApoloApps Indeed, it was not adopted for wasm. Can you open a new issue please?

sandwwraith avatar Sep 19 '25 16:09 sandwwraith