Skip to content

Instantly share code, notes, and snippets.

@popmedic
Last active April 25, 2020 17:40
Show Gist options
  • Save popmedic/4f003c2df3d668efa2dee6f1206f6aa2 to your computer and use it in GitHub Desktop.
Save popmedic/4f003c2df3d668efa2dee6f1206f6aa2 to your computer and use it in GitHub Desktop.
Plist vs. JSON for converting Encodable implementation to Any (Dictionary)
import Foundation
struct LotsOfStuff: Codable {
let x: Int
let y: Int
let z: Double
let a: [String]
let b: String
let c: String
let less: LessStuff
}
struct LessStuff: Codable {
let x: Int
let y: Int
let a: String
}
var object = LotsOfStuff(x: 1, y: 2, z: 3.0,
a: ["this", "is", "a", "test"],
b: "this is a test", c: "of the emergancy broadcast system",
less: LessStuff(x: 4, y: 5, a: "I ❤️ Denver"))
do {
var start: TimeInterval
start = Date().timeIntervalSince1970
for _ in [0..<1000000] {
_ = try JSONSerialization.jsonObject(with: try JSONEncoder().encode(object))
}
print("json took: \(Date().timeIntervalSince1970 - start)")
start = Date().timeIntervalSince1970
for _ in [0..<1000000] {
_ = try PropertyListSerialization.propertyList(from: try PropertyListEncoder().encode(object),
options: [], format: nil)
}
print("plist took: \(Date().timeIntervalSince1970 - start)")
} catch {
print("error: \(error.localizedDescription)")
exit(1)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment