In iOS development, a lot of people use SBJSON (also known as JSON-Framework) to do JSON parsing libraries. I think this is because SBJSON was the first JSON parsing library to appear on iOS. However, with the popularity of iOS development, more and more excellent JSON parsing libraries have emerged, and SBJSON has a big performance gap compared to them.

Currently, the main popular JSON parsing libraries in the iOS industry include NSJSONSerialization, NextiveJson, TouchJSON, SBJSON, YAJL, and JSONKit

JSON parsing API has been added to iOS API. We have tested its parsing speed with other five open source JSON parsing libraries. The test results and project code attachments are as follows.

We selected four files containing data in JSON format for testing. Each file is parsed for 100 times, and the parsed time is compared.

The project contains the following files and frameworks:

The code macro for the test interval is defined as follows, where the count and parse code are passed in from an external call:

#define RunWithCount(count, description, expr) \
do{\CFAbsoluteTime start = CFAbsoluteTimeGetCurrent(a); \for(NSInteger i = 0; i < count; i++) { \ expr; \ \ \}CFTimeInterval took = CFAbsoluteTimeGetCurrent() - start; \
NSLog(@ "% @ % 0.3 f", description, took); \ \}while (0)
Copy the code

This is the code called outside to set the number of times the json file is read and evaluated. Each function is parsed using the corresponding framework API:

    JSONTest *test = [[JSONTest alloc] init];
    
    NSInteger count = 100;
    
    [test runWithResourceName:@"twitter_public.json" count:count];
    NSLog(@"\n\n\n");
    [test runWithResourceName:@"lastfm.json" count:count];
    NSLog(@"\n\n\n");
    [test runWithResourceName:@"delicious_popular.json" count:count];
    NSLog(@"\n\n\n");
    [test runWithResourceName:@"yelp.json" count:count];
    NSLog(@"\n\n\n");
Copy the code

Our test environment is Xcode 8.2.1 and iOS10. The calculation times are 100. Here is the result of calculation Log:

2017-01-17 16:42:34.557 JSONParseTest[21468:1030203] nsJSonSerialization-twitter_public.json 0.029 2017-01-17 16:42:34.604 JSONParseTest[21468:1030203] yajl-twitter_public.json 0.046 2017-01-17 16:42:34.658 JSONParseTest[21468:1030203] nextivejson-twitter_public. Json 0.053 2017-01-17 16:42:34.715 JSONParseTest[21468:1030203] Jsonkit-twitter_public.json 0.056 2017-01-17 16:42:34.834 JSONParseTest[21468:1030203] TouchJSON-twitter_public.json 0.118 2017-01-17 16:42:35.098 JSONParseTest[21468:1030203] sbJSON-twitter_public. Json 0.264 2017-01-17 16:42:35.145 JSONParseTest[21468:1030203] nsjsonserialization-lastfm. json 0.044 2017-01-17 16:42:35.208 JSONParseTest[21468:1030203] JSONParseTest[21468:1030203] nextivejson-lastfm. Json 0.089 2017-01-17 JSONParseTest[21468:1030203] nextivejson-lastfm JSONKit [21468:1030203] jsonKit-lastfm.json 0.089 2017-01-17 16:42:35.602 JSONParseTest[21468:1030203] JSONParseTest[21468:1030203] sbjson-lastfm. json 0.409 2017-01-17 16:42:36.064 JSONParseTest[21468:1030203] nsjSonserialization-delicious_popular.json 0.050 2017-01-17 16:42:36.119 JSONParseTest[21468:1030203] yajl-delicious_popular.json 0.054 2017-01-17 16:42:36.193 JSONParseTest[21468:1030203] Nextivejson-delicious_popular. Json 0.073 2017-01-17 16:42:36.275 JSONParseTest[21468-1030203] Jsonkit-delicious_popular.json 0.082 2017-01-17 16:42:36.430 JSONParseTest[21468:1030203] Json 0.154 2017-01-17 16:42:36.771 JSONParseTest[21468:1030203] JSONParseTest[21468:1030203] NSJSONSerialization- Yelp.json JSONParseTest[21468:1030203] yajl-yelp.json 0.047 2017-01-17 16:42:36.922 JSONParseTest[21468:1030203] nextivejson-yelp. Json 0.065 2017-01-17 16:42:37.001 JSONParseTest[21468:1030203] Jsonkit-yelp.json 0.078 2017-01-17 16:42:37.139 JSONParseTest[21468:1030203] Touchjson-yelp.json 0.137 2017-01-17 16:42:37. 487 JSONParseTest [21468-1030203] an SBJSON - yelp. Json 0.347Copy the code

Put the above data into the chart below:

The test results show that the API of the system has the fastest parsing speed. We choose to use it in the engineering project, and SBJSON, which is widely used, has the second worst parsing speed, which makes me surprised. A closer approximation of the system API would be YAJL.

There is no comparison of API’s open interface and usage mode. If the test is purely based on the above analysis speed: 1: try to choose the API of the system for testing; 2: YAJL is recommended for using a third party

#### Conclusion (parity values of the four sets of data):

  1. NSJSONSerialization (0.05325)
  2. YAJL (0.07325)
  3. NextiveJson (0.09875)
  4. JSONKit (0.10325)
  5. TouchJSON (0.21575)
  6. An SBJSON (0.448)

Program attachment link: https://github.com/zhongad/JSONParseTest