CSVTable: Fast and Easy CSV Parsing for JavaScript
CSVTable is a lightweight JavaScript library designed to parse CSV data quickly and render it as HTML tables or structured JavaScript objects. It focuses on ease of use, performance for moderately large files, and flexible output options for web apps and Node.js scripts.
Key features
- Fast parsing: Optimized tokenizer handles commas, quoted fields, escaped quotes, and newlines with minimal overhead.
- Flexible input: Accepts CSV strings, File/Blob (browser), or streams (Node.js).
- Multiple outputs: Returns arrays of objects (header-based), arrays of arrays, or builds an HTML table element directly.
- Configurable: Options for custom delimiters, newline normalization, header row handling, and trimming whitespace.
- Streaming support: Parse large files chunk-by-chunk to reduce memory usage.
- Lightweight: Small bundle size with no external dependencies.
- Type inference (optional): Convert numeric and boolean-looking fields automatically.
- Error reporting: Row/column-aware parse errors with line numbers.
Basic usage (browser)
javascript
// parse CSV string to objects (first row = headers) const csv = ‘name,age Alice,30 Bob,25’; const rows = CSVTable.parse(csv); // [{name: ‘Alice’, age: 30}, {name: ‘Bob’, age: 25}] // render to an HTML table const table = CSVTable.toTable(csv); document.body.appendChild(table);
Basic usage (Node.js, streaming)
javascript
const fs = require(‘fs’); const stream = fs.createReadStream(‘data.csv’); CSVTable.parseStream(stream, {delimiter: ’,’, onRow: row => { // process each row object as it’s parsed console.log(row); }});
Configuration options
- delimiter: string (default “,”)
- header: boolean | number (default true — first row is header; or numeric row index)
- trim: boolean (default true)
- skipEmptyLines: boolean (default true)
- inferTypes: boolean (default false)
- chunkSize: number (for streaming)
- onRow: function (callback for each parsed row)
Performance notes
- Best for files up to several hundred MB with streaming enabled; for multi-GB files, use dedicated CSV parsers optimized in native code.
- Enabling type inference slightly increases CPU usage; disable if raw strings are preferred.
When to use CSVTable
- Quickly display CSV data in web apps.
- Lightweight parsing in client-side tools.
- Node.js scripts that need readable streaming CSV parsing without heavy dependencies.
Alternatives
- PapaParse — robust browser/Node parsing with worker support.
- csv-parse (Node) — full-featured, stream-friendly parser for large-scale processing.
Leave a Reply