0

I am trying to seek the way making more performant deserialization process in our application. I created simple test below. When I check the results, it seems like JsonConvert.DeserializeObject working much faster after first iteration.

[TestMethod]
        public void DeserializeObjectTest()
        {
            int count = 5;
            for (int i = 0; i < count; i++)
            {
                CookieCache cookieCache = new CookieCache()
                {
                    added = DateTime.UtcNow.AddDays(-1),
                    VisitorId = Guid.NewGuid().ToString(),
                    campaigns = new List<string>() { "qqq", "www", "eee" },
                    target_dt = "3212018",
                    updated = DateTime.UtcNow

                };
                Stopwatch stopwatch = Stopwatch.StartNew();
                string serializeObject = JsonConvert.SerializeObject(cookieCache);
                CookieCache deserializeObject = JsonConvert.DeserializeObject<CookieCache>(serializeObject);
                stopwatch.Stop();
                double stopwatchElapsedMilliseconds = stopwatch.Elapsed.TotalMilliseconds;
                Debug.WriteLine("iteration " + i + ": " + stopwatchElapsedMilliseconds);
            }

And my results:

enter image description here

I think I am using Stopwatch correctly. So does JSON.NET is using some sort of internal caching or optimization process on deserialization on following calls?

Since this is a web application, of course, I am getting similar results in my logs (280.6466) for every single web request.

So am I missing something on my test? Or is it expected behavior?

Teoman shipahi
  • 47,454
  • 15
  • 134
  • 158
  • could be due to .NET reflection loading or branch prediction. I would say try to iterate thru all the property and value using reflection first and then try this test after to see – Steve Mar 21 '18 at 15:45
  • 1
    Json.NET caches type information as described in [Does Json.NET cache types' serialization information?](https://stackoverflow.com/q/33557737/3744182). 1000x slower is unexpected however. Have you profiled? – dbc Mar 21 '18 at 15:45
  • Well sure it does. It wouldn't be the most widely used json serializer if it didn't make even such trivial optimizations. – Evk Mar 21 '18 at 15:54
  • @Evk well, would it be also expected to see same way on web applications? Because on our performance logs, seems like every single request is taking similar amount of time serialization/deserialization for same class type. In this case, would it be possible JsonConvert serialization cache happens only current request context but not on application lifetime? – Teoman shipahi Mar 21 '18 at 16:01
  • 1
    @Teomanshipahi that's unlikely. I didn't completely understand the question. It seems you mean that on every request first deserialization is slow (like 280ms in your example). I'd say it's not an expected behavior. And what web server \ asp.net version? – Evk Mar 21 '18 at 16:04
  • @Evk I made my tests again, and yes you're right. JSON.NET keeps optimization during Web App Life-cycle. So I will re-do my profiling again. – Teoman shipahi Mar 22 '18 at 13:31

0 Answers0