Search code examples
iosjsonswiftjsondecoder

JSON Parsing using multiple cores


iOS devices are getting better, have more cores but how can we get benefit out of it while we are parsing JSON?

Currently, I am using JSONDecoder() for JSON Parsing. Is there way we can do it faster? Maybe using multiple threads parsing in parts etc.

Any hints/pointers will be appreciated.

    import Foundation
    
    let filePath = Bundle.main.path(forResource: "json", ofType: "json")
    
    struct Vehicle: Codable {
        private let color: String
        private let tires: [Tire]
        private let number: String
    }
    
    struct Tire: Codable {
        private let company: String
        private let isNew: Bool
    }
    
    func parseData(_ data: Data) {
        let decoder = JSONDecoder()
        try? decoder.decode([Vehicle].self, from: data)
    }
    
    func modifiedParsing(_ data: Data) {
        
    }
    
    let data = try String(contentsOfFile: filePath!).data(using: .utf8)
    
    let date = Date()
    let start = date.timeIntervalSince1970
    
    //parseData(data!)
    
    let end = Date().timeIntervalSince1970
    
    print("Time Taken \(end-start)")
    
    /*
     Initial Times: [3.728722095489502, 3.5913820266723633, 3.5568389892578125, 3.534559965133667, 3.506725311279297]
     After Changes
     */

Solution

  • I wanted to make JSON Parsing faster. For anyone who is looking for a good solution please refer: https://github.com/bwhiteley/JSONShootout

    Marshal is faster than codable.