1

I have a really big chunk of JSON i'm deserializing with Newtonsoft.json

Users.UserDataDict response = JsonConvert.DeserializeObject<Users.UserDataDict>(teststring);

I notice that the first time I call this method it's really slow (hundreds of milliseconds longer than it needs to be). So what I'm doing is calling it when the app starts on some large dummy data in so that when the user is interacting with the app it's not slow. Is there anyway I can just initialize it properly or increase it's buffer size or whatever needs done rather than calling it on start up on some really large data?

LampShade
  • 2,675
  • 5
  • 30
  • 60
  • Here are some tips on how to improve performance when working with big JSON data: http://www.newtonsoft.com/json/help/html/performance.htm – Tarzan Aug 12 '16 at 20:17
  • The first time you deserialize, required DLLs might be loaded; various methods might get JITted; type initializers might get called; and Json.NET will generate and cache contract information as described [here](https://stackoverflow.com/questions/33557737). To "warm up" the serializer you don't need to deserialize a *large* chunk of dummy data, you probably just need to generate contracts for each type to be deserialized. – dbc Aug 12 '16 at 20:42
  • If you have control over the API that you are consuming the service from, you can paginate the data so that you increase throughput. The data will come in pages of 20 items per page. So you can get the first 20 items, display them to the user then load the rest and add to the initial list. – Timothy Macharia Aug 12 '16 at 20:59
  • Thanks for your help guys! – LampShade Aug 16 '16 at 16:54

1 Answers1

1

Seems that JsonConvert caches each type to be de-/serialized. Based on that the first call to JsonConvert.SerializeObject/JsonConvert.DeserializeObject may take "longer". A more detailed answer can be found here.

Community
  • 1
  • 1
Moerwald
  • 10,448
  • 9
  • 43
  • 83