Performance comparison of Unity NewtonsoftJson, LitJson and SimpleJSON

Performance comparison test of Json Library in Unity

Class library size comparison:

Class library file type Size
NewtonsoftJson .dll 353KB
LitJson .dll 56KB
SimpleJSON .cs 68KB

Analysis time comparison:
Execution times: 10000 times

test method NewtonsoftJson LitJson SimpleJSON
Test 1 114ms 158ms 52ms
Test 2 136ms 288ms 126ms
Test 3 263ms 542ms 169ms
Test 4 333ms 747ms 200ms

Test code:

using UnityEngine;
using System.Diagnostics;
using LitJson;
using SimpleJSON;
using Newtonsoft.Json.Linq;

/// <summary>
/// JsonTest
/// ZhangYu 2019-07-11
///< para > Article address: https://segmentfault.com/a/119000019731298 < / para >
/// </summary>
public class JsonTest : MonoBehaviour {

    public int count = 10000;
    private Stopwatch watch;

    private void Start () {
        watch = new Stopwatch();

        string json1 = "{\"id\":10001,\"name\":\"test\"}";
        string json2 = "[1,2,3,4,5,6,7,8,9,10]";
        string json3 = "{\"id\":10000,\"username\":\"zhangyu\",\"password\":\"123456\",\"nickname\":\"Iced Baidu\",\"age\":20,\"gender\":1,\"phone\":12345678910,\"email\":\"zhangyu@xx.com\"}";
        string json4 = "[\"test2\",[[\"key1\",    \"id\"],[\"key2\",    \"hp\"],[\"key3\",    \"mp\"],[\"key4\",    \"exp\"],[\"key5\",    \"money\"],[\"key6\",    \"point\"],[\"key7\",    \"age\"],[\"key8\",    \"sex\"]]]";

        JsonParseTest(json1);
        JsonParseTest(json2);
        JsonParseTest(json3);
        JsonParseTest(json4);
    }

    private void JsonParseTest(string json) {
        print("json:" + json);
        bool isArray = json[0] == '[';
        NewtonsoftJsonTest(json, isArray);
        LiteJsonTest(json);
        SimpleJsonTest(json);
        print("======================");
    }

    private void NewtonsoftJsonTest(string json, bool isArray) {
        watch.Reset();
        watch.Start();
        if (isArray) {
            for (int i = 0; i < count; i++) {
                JArray jArray = JArray.Parse(json);
            }
        } else {
            for (int i = 0; i < count; i++) {
                JObject jObj = JObject.Parse(json);
            }
        }
        watch.Stop();
        print("NewtonsoftJson Parse Time(ms):" + watch.ElapsedMilliseconds);
    }

    private void LiteJsonTest(string json) {
        watch.Reset();
        watch.Start();
        for (int i = 0; i < count; i++) {
            JsonData jData = JsonMapper.ToObject(json);
        }
        watch.Stop();
        print("LiteJson Parse Time(ms):" + watch.ElapsedMilliseconds);
    }

    private void SimpleJsonTest(string json) {
        watch.Reset();
        watch.Start();
        for (int i = 0; i < count; i++) {
            JSONNode jNode = JSON.Parse(json);
        }
        watch.Stop();
        print("SimpleJson Parse Time(ms):" + watch.ElapsedMilliseconds);
    }

}

Conclusion:
SimpleJSON wins!
SimpleJSON wins with the smallest size, fastest speed and easiest integration
SimpleJSON is continuously optimized for Unity, with fast update, small size, fast speed and source code. SimpleJSON is recommended.
NewtonsoftJson was eliminated because of its large size and slowest speed. In principle, such a large class library must support more functions. Unfortunately, the existing project is not too complex json and cannot be tested.
LitJson is also small and convenient to use, but it has no advantage in speed.

Github address of SimpleJSON: https://github.com/Bunny83/Si...

Keywords: C# JSON Unity github

Added by angel1987 on Wed, 30 Oct 2019 19:21:30 +0200