fastest-csv
v0.2.1
Published
Fastest CSV parser — zero dependencies, 100+ lines, <1KB gzip
Maintainers
Readme
fastest-csv ⚡
轻量,极速,0依赖。 Lightweight, blazing fast, zero dependencies.
Benchmark
12.4MB CSV, 100000 rows, 10 cols (50% "quotes")
fastest-csv fastMode █████ 162ms 🥇🏆
fastest-csv ████████████ 352ms 🥉
PapaParse fastMode ███████████ 328ms 2.0x slower
PapaParse ██████████████ 411ms 2.5x slower
csv-parse ████████████████████████████ 836ms 5.2x slowerfastMode (不解析引号 - no quote parsing)
Quick Start
npm install fastest-csvimport { parseCSV } from 'fastest-csv'
const { headers, rows } = parseCSV(`name,age,city
Alice,30,New York
Bob,25,London`)
// headers: ['name', 'age', 'city']
// rows: [
// { name:'Alice', age:'30', city:'New York' },
// { name:'Bob', age:'25', city:'London' }
// ]API
parseCSV(text, options?)| 参数Param | 类型Type | 默认值Default | 说明Description |
| ------------- | --------------------- | ----------------- | --------------------------------------------------------- |
| text | string | '' | CSV 文本CSV text |
| separator | string | ',' | 列分隔符,任意单字符Column delimiter, any single char |
| fastMode | boolean | false | 不解析引号,性能最佳Skip quote parsing, fastest mode |
| headers | string[] | [] | 预设表头Preset headers |
| onHeader | (v, k, i) => string | String | 表头转换钩子Header transform hook |
| onValue | (v, k, i) => any | String | 值转换钩子Value transform hook |
return { headers: string[], rows: Record<string, any>[] }
Recipes
// 自动转类型 - Auto-type on parse
parseCSV('name,age\nAlice,18', {
onValue: (v) => (isNaN(v) ? v : Number(v)),
})
// 重命名表头 - Rename headers
parseCSV('name,age\nAlice,18', {
onHeader: (h) => h.toLowerCase(),
})
// 预设表头 - Preset headers
parseCSV('Alice,18', { headers: ['name', 'age'] })
// 任意分隔符 - Any delimiter
parseCSV('a;b;c\n1;2;3', { separator: ';' })
// fastMode:不解析引号,性能最佳 - Fastest mode (no quote parsing)
parseCSV('a,b\n1,2', { fastMode: true })Features
- ✅ 标准 CSV - Standard CSV
- ✅ 任意分隔符 - Any delimiter
- ✅ 引号字段 - Quoted fields
- ✅ 转义引号
""→"- Escaped quotes - ✅ 引号内换行 - Newlines in quotes
- ✅ 换行符全兼容
\n\r\n\r- All line endings - ✅ 转换钩子
onHeaderonValue- Transform hooks - ✅ 极速模式(不解析引号)- Fastest mode (no quote parsing)
- ✅ 零依赖 - Zero dependencies
- ✅ 浏览器 / Node.js / Deno / Bun - Any JS runtime
- ✅ ES Module
- ✅ TypeScript
Test
# npm run test
node --test tests/fastest-csv.test.js全部代码分支覆盖,全绿。 All branches covered, all green.
License
MIT
