Skip to content

Instantly share code, notes, and snippets.

@ricardopereira
Forked from dimitribouniol/main.swift
Created December 28, 2025 21:43
Show Gist options
  • Select an option

  • Save ricardopereira/676ecca3d464e0c329edded2448a9f57 to your computer and use it in GitHub Desktop.

Select an option

Save ricardopereira/676ecca3d464e0c329edded2448a9f57 to your computer and use it in GitHub Desktop.
ISO8601 Date Formatter Benchmarks
import AppKit
@main
struct DateFormatters {
static func main() {
let iterations = 100_000
let startInterval: TimeInterval = 780_000_000
time("Create Date Baseline") {
var results: [Date] = []
results.reserveCapacity(iterations)
for offset in 0..<iterations {
let date = Date(timeIntervalSinceReferenceDate: startInterval + TimeInterval(offset))
results.append(date)
}
print("\(results.count) - \(results.first.map { $0.description } ?? "nil")")
}
time("Cached Date.ISO8601FormatStyle Format") {
let formatter = Date.ISO8601FormatStyle(includingFractionalSeconds: true)
var results: [String] = []
results.reserveCapacity(iterations)
for offset in 0..<iterations {
let date = Date(timeIntervalSinceReferenceDate: startInterval + TimeInterval(offset))
results.append(date.formatted(formatter))
}
print("\(results.count) - \(results.first.map { $0.description } ?? "nil")")
}
time("Lazy Date.ISO8601FormatStyle Format") {
var results: [String] = []
results.reserveCapacity(iterations)
for offset in 0..<iterations {
let date = Date(timeIntervalSinceReferenceDate: startInterval + TimeInterval(offset))
results.append(date.formatted(.iso8601
.year()
.month()
.day()
.timeZone(separator: .omitted)
.time(includingFractionalSeconds: true)
.timeSeparator(.colon)
))
}
print("\(results.count) - \(results.first.map { $0.description } ?? "nil")")
}
time("Cached Date.ISO8601FormatStyle Parse") {
let formatter = Date.ISO8601FormatStyle(includingFractionalSeconds: true)
var results: [Date?] = []
results.reserveCapacity(iterations)
for offset in 0..<iterations {
let date = Date(timeIntervalSinceReferenceDate: startInterval + TimeInterval(offset))
let string = date.formatted(formatter)
results.append(try? formatter.parse(string))
}
print("\(results.count) - \(results.first?.map { $0.description } ?? "nil")")
}
time("Lazy Date.ISO8601FormatStyle Parse") {
var results: [Date?] = []
results.reserveCapacity(iterations)
for offset in 0..<iterations {
let date = Date(timeIntervalSinceReferenceDate: startInterval + TimeInterval(offset))
let string = date.formatted(.iso8601
.year()
.month()
.day()
.timeZone(separator: .omitted)
.time(includingFractionalSeconds: true)
.timeSeparator(.colon)
)
results.append(try? Date(string, strategy: .iso8601
.year()
.month()
.day()
.timeZone(separator: .omitted)
.time(includingFractionalSeconds: true)
.timeSeparator(.colon)
))
}
print("\(results.count) - \(results.first?.map { $0.description } ?? "nil")")
}
time("Cached ISO8601DateFormatter Format") {
let formatter = ISO8601DateFormatter()
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.formatOptions = [
.withInternetDateTime,
.withDashSeparatorInDate,
.withColonSeparatorInTime,
.withTimeZone,
.withFractionalSeconds
]
var results: [String] = []
results.reserveCapacity(iterations)
for offset in 0..<iterations {
let date = Date(timeIntervalSinceReferenceDate: startInterval + TimeInterval(offset))
results.append(formatter.string(from: date))
}
print("\(results.count) - \(results.first.map { $0.description } ?? "nil")")
}
time("Lazy ISO8601DateFormatter Format") {
var results: [String] = []
results.reserveCapacity(iterations)
for offset in 0..<iterations {
let formatter = ISO8601DateFormatter()
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.formatOptions = [
.withInternetDateTime,
.withDashSeparatorInDate,
.withColonSeparatorInTime,
.withTimeZone,
.withFractionalSeconds
]
let date = Date(timeIntervalSinceReferenceDate: startInterval + TimeInterval(offset))
results.append(formatter.string(from: date))
}
print("\(results.count) - \(results.first.map { $0.description } ?? "nil")")
}
time("Cached ISO8601DateFormatter Parse") {
let formatter = ISO8601DateFormatter()
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.formatOptions = [
.withInternetDateTime,
.withDashSeparatorInDate,
.withColonSeparatorInTime,
.withTimeZone,
.withFractionalSeconds
]
var results: [Date?] = []
results.reserveCapacity(iterations)
for offset in 0..<iterations {
let date = Date(timeIntervalSinceReferenceDate: startInterval + TimeInterval(offset))
let string = formatter.string(from: date)
results.append(formatter.date(from: string))
}
print("\(results.count) - \(results.first?.map { $0.description } ?? "nil")")
}
time("Lazy ISO8601DateFormatter Parse") {
var results: [Date?] = []
results.reserveCapacity(iterations)
for offset in 0..<iterations {
let formatter = ISO8601DateFormatter()
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.formatOptions = [
.withInternetDateTime,
.withDashSeparatorInDate,
.withColonSeparatorInTime,
.withTimeZone,
.withFractionalSeconds
]
let date = Date(timeIntervalSinceReferenceDate: startInterval + TimeInterval(offset))
let string = formatter.string(from: date)
results.append(formatter.date(from: string))
}
print("\(results.count) - \(results.first?.map { $0.description } ?? "nil")")
}
}
static func time(_ name: String, handler: () -> ()) {
let startTime = CACurrentMediaTime()
handler()
let endTime = CACurrentMediaTime()
print("\(name) took \((endTime - startTime).formatted(.number.precision(.fractionLength(3))))s")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment