Hello everyone.
A while ago, I was looking for a CSV parser in Swift. While there are a few popular out there for Swift that are pretty good, I found them to be a little bit slow, so decided to try to write my own. Then I fell into a rabbit hole of trying to squeeze as much performance as I could out of it. I was quickly able to outperform SwiftCSV which appears to be the most popular library for swift, but surprisingly it took me a while to beat the Python CSV parser.
I was pretty happy with the results and thought it might be worthwhile to spend some effort to make it usable for others. The result is FastCSV. I've tried to make it as swifty as I could. Here are some of the features.
Reading
-
Decodable support โ decode CSV rows directly into Swift structs, only materializing the columns you need
-
Column mapping โ map CSV headers to struct properties at the call site, no CodingKeys required
-
High-performance parsing with zero-copy techniques
-
Low memory footprint through chunked streaming โ constant memory regardless of file size
-
Three API tiers โ typed structs via
Decodable, dictionary access by column name, or raw array iteration -
Configurable delimiters supporting standard CSV, TSV, and custom formats
-
Quote handling with optional optimization for quote-free data
-
Error recovery allowing processing to continue despite malformed rows
-
UTF-8 BOM detection and automatic removal
Writing
-
Encodable support โ write Swift structs directly to CSV with automatic header derivation
-
RFC 4180 quoting โ fields containing delimiters, quotes, or newlines are quoted automatically
-
Multiple output targets โ write to file path, URL, or string
-
Row-by-row or batch โ streaming writes via
CSVWriteror one-shot via static methods -
Round-trip fidelity โ read, transform, and write back with full type preservation
A few things I'm interested in adding to it in the future are support for compressed CSV files and exploring SIMD to try to improve performance further.
Hope this will be of use to somebody. The repo is at GitHub - wombat2k/FastCSV: FastCSV is a high-performance CSV (Comma-Separated Values) parser written in Swift ยท GitHub.