Performance comparison test of Json Library in Unity
Class library size comparison:
class libraries | file type | Size |
---|---|---|
NewtonsoftJson | .dll | 353KB |
LitJson | .dll | 56KB |
SimpleJSON | .cs | 68KB |
Analytical time comparison:
Number of executions: 10,000
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>Blog:https://segmentfault.com/a/1190000019731298</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":\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\[45678910, "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 by its smallest volume, fastest speed and easiest integration.
SimpleJSON for Unity in continuous optimization, update faster, and small size, fast speed, active code, recommended SimpleJSON
Newton software Json was eliminated because of its large size. It is reasonable to say that such a large class library will support more functions. Unfortunately, the existing projects are not too complex to verify.
LitJson is also compact and easy to use. Unfortunately, speed has no advantage at all.
SimpleJSON Github address: https://github.com/Bunny83/Si…