JSONSerialization performance between swift-corelibs-foundation and iOS foundation

How I test the performance:

  1. Create an application with Xcode.
  2. Copy the JSONSerialization.swift into the project and change the name to OPJSONSerialization to avoid name collision
  3. Test with below code and run on iPhone X, iOS 13.2:
        let path = Bundle.main.path(forResource: "test", ofType: "json")
        if let data = FileManager.default.contents(atPath: path!) {
            let st = Date().timeIntervalSince1970
            _ = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers)
            let et = Date().timeIntervalSince1970
            testResultLabel.text = "\(et - st)"
            let st2 = Date().timeIntervalSince1970
            _ = try? OPJSONSerialization.jsonObject(with: data, options: .mutableLeaves)
            let et2 = Date().timeIntervalSince1970
            testResultLabel2.text = "\(et2 - st2)"

The file data count is 527953.
The costs on average are 0.014 vs. 0.38. The performance is 27 times worse.

Why the performance is so poor for the opensource version

First, what build settings are you using in your project? Since you're comparing a system framework (which is always optimized) to code compiled into your app, are you making sure that your code is being compiled with similar optimizations enabled?

The results are still likely to be different since the open-source version is written in Swift while the system framework is C/Objective-C, but let's make sure you're comparing apples to apples first.


Hey Tony, thanks for your reminder. Set the optimization level to -O and tested again, the result looks good now:
0.011 vs 0.051 on average.

Terms of Service

Privacy Policy

Cookie Policy